oidc-jwt-client
Advanced tools
Comparing version 2.0.1-develop.1 to 2.1.0-develop.1
@@ -796,6 +796,6 @@ import React, { useReducer, useRef, useEffect, useLayoutEffect, useContext, useCallback, useState } from 'react'; | ||
methods: { | ||
setIsLoggedIn: function (loggedIn) { | ||
setIsLoggedIn: function (isLoggedIn) { | ||
if (!isSSR) | ||
localStorage.setItem(LOGGED_IN_TOKEN_STORAGE_KEY, JSON.stringify(loggedIn)); | ||
set(__assign({ isLoggedIn: loggedIn }, (!loggedIn ? { userInfoCache: undefined } : {}))); | ||
localStorage.setItem(LOGGED_IN_TOKEN_STORAGE_KEY, JSON.stringify(isLoggedIn)); | ||
set(__assign({ isLoggedIn: isLoggedIn }, (!isLoggedIn ? { userInfoCache: undefined } : {}))); | ||
}, | ||
@@ -835,14 +835,31 @@ fetchJsonWithAuth: function (url) { | ||
}, | ||
loadInitialData: function () { | ||
var _a = get().methods, receiveSessionToken = _a.receiveSessionToken, getAccessToken = _a.getAccessToken; | ||
return receiveSessionToken().then(function (data) { | ||
if (data) { | ||
return getAccessToken().then(function (info) { var _a; return (_a = info === null || info === void 0 ? void 0 : info.claims) !== null && _a !== void 0 ? _a : null; }); | ||
} | ||
return data; | ||
}); | ||
}, | ||
receiveSessionToken: function (redirect) { | ||
var _this = this; | ||
if (redirect === void 0) { redirect = true; } | ||
var _a = get().methods, setSessionToken = _a.setSessionToken, getUserInfo = _a.getUserInfo; | ||
var match = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || []; | ||
var _b = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || [], token = _b[1]; | ||
var getUserInfoPromise = getUserInfo(); | ||
if (!(match === null || match === void 0 ? void 0 : match.length)) | ||
return; | ||
setSessionToken(match[1]); | ||
var userInfoHandler = redirect ? function () { | ||
window.location.href = stripTokenFromUrl(window.location.href).replace(/\?$/, '').replace(/#\.$/, ''); | ||
} : function () { return null; }; | ||
getUserInfoPromise | ||
if (!token) | ||
return getUserInfoPromise; | ||
setSessionToken(token); | ||
var userInfoHandler = redirect ? function (data) { return __awaiter(_this, void 0, void 0, function () { | ||
var urlWithoutToken; | ||
return __generator(this, function (_a) { | ||
urlWithoutToken = stripTokenFromUrl(window.location.href).replace(/\?$/, '').replace(/#\.$/, ''); | ||
window.history.replaceState({}, '', urlWithoutToken); | ||
return [2 /*return*/, Promise.resolve(data)]; | ||
}); | ||
}); } : function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, Promise.resolve(null)]; | ||
}); }); }; | ||
return getUserInfoPromise | ||
.then(userInfoHandler) | ||
@@ -984,3 +1001,3 @@ .catch(userInfoHandler); | ||
var OidcJwtProvider = function (props) { | ||
var options = props.client, _a = props.shouldAttemptLogin, shouldAttemptLogin = _a === void 0 ? false : _a, _b = props.shouldMonitorAccessTokens, shouldMonitorAccessTokens = _b === void 0 ? true : _b, children = props.children; | ||
var options = props.client, _a = props.shouldAttemptLogin, shouldAttemptLogin = _a === void 0 ? false : _a, _b = props.shouldMonitorAccessTokens, shouldMonitorAccessTokens = _b === void 0 ? true : _b, onInitialized = props.onInitialized, children = props.children; | ||
var contextRef = useRef(); | ||
@@ -993,8 +1010,11 @@ if (!contextRef.current) { | ||
var useStore = contextRef.current.useStore; | ||
var _c = useStore(function (state) { return state.methods; }), authorize = _c.authorize, receiveSessionToken = _c.receiveSessionToken, monitorAccessToken = _c.monitorAccessToken, stopMonitoringAccessToken = _c.stopMonitoringAccessToken; | ||
var _c = useStore(function (state) { return state.methods; }), authorize = _c.authorize, loadInitialData = _c.loadInitialData, monitorAccessToken = _c.monitorAccessToken, stopMonitoringAccessToken = _c.stopMonitoringAccessToken; | ||
var isLoggedIn = useStore(function (state) { return state.isLoggedIn; }); | ||
var isCSRFTokenPresent = !!useStore(function (state) { return state.csrfToken; }); | ||
useEffect(function () { | ||
receiveSessionToken(); | ||
}, [receiveSessionToken]); | ||
loadInitialData().then(function (data) { | ||
if (onInitialized) | ||
onInitialized(data); | ||
}); | ||
}, [loadInitialData, onInitialized]); | ||
useEffect(function () { | ||
@@ -1001,0 +1021,0 @@ if (!isLoggedIn) |
@@ -804,6 +804,6 @@ 'use strict'; | ||
methods: { | ||
setIsLoggedIn: function (loggedIn) { | ||
setIsLoggedIn: function (isLoggedIn) { | ||
if (!isSSR) | ||
localStorage.setItem(LOGGED_IN_TOKEN_STORAGE_KEY, JSON.stringify(loggedIn)); | ||
set(__assign({ isLoggedIn: loggedIn }, (!loggedIn ? { userInfoCache: undefined } : {}))); | ||
localStorage.setItem(LOGGED_IN_TOKEN_STORAGE_KEY, JSON.stringify(isLoggedIn)); | ||
set(__assign({ isLoggedIn: isLoggedIn }, (!isLoggedIn ? { userInfoCache: undefined } : {}))); | ||
}, | ||
@@ -843,14 +843,31 @@ fetchJsonWithAuth: function (url) { | ||
}, | ||
loadInitialData: function () { | ||
var _a = get().methods, receiveSessionToken = _a.receiveSessionToken, getAccessToken = _a.getAccessToken; | ||
return receiveSessionToken().then(function (data) { | ||
if (data) { | ||
return getAccessToken().then(function (info) { var _a; return (_a = info === null || info === void 0 ? void 0 : info.claims) !== null && _a !== void 0 ? _a : null; }); | ||
} | ||
return data; | ||
}); | ||
}, | ||
receiveSessionToken: function (redirect) { | ||
var _this = this; | ||
if (redirect === void 0) { redirect = true; } | ||
var _a = get().methods, setSessionToken = _a.setSessionToken, getUserInfo = _a.getUserInfo; | ||
var match = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || []; | ||
var _b = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || [], token = _b[1]; | ||
var getUserInfoPromise = getUserInfo(); | ||
if (!(match === null || match === void 0 ? void 0 : match.length)) | ||
return; | ||
setSessionToken(match[1]); | ||
var userInfoHandler = redirect ? function () { | ||
window.location.href = stripTokenFromUrl(window.location.href).replace(/\?$/, '').replace(/#\.$/, ''); | ||
} : function () { return null; }; | ||
getUserInfoPromise | ||
if (!token) | ||
return getUserInfoPromise; | ||
setSessionToken(token); | ||
var userInfoHandler = redirect ? function (data) { return __awaiter(_this, void 0, void 0, function () { | ||
var urlWithoutToken; | ||
return __generator(this, function (_a) { | ||
urlWithoutToken = stripTokenFromUrl(window.location.href).replace(/\?$/, '').replace(/#\.$/, ''); | ||
window.history.replaceState({}, '', urlWithoutToken); | ||
return [2 /*return*/, Promise.resolve(data)]; | ||
}); | ||
}); } : function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, Promise.resolve(null)]; | ||
}); }); }; | ||
return getUserInfoPromise | ||
.then(userInfoHandler) | ||
@@ -992,3 +1009,3 @@ .catch(userInfoHandler); | ||
var OidcJwtProvider = function (props) { | ||
var options = props.client, _a = props.shouldAttemptLogin, shouldAttemptLogin = _a === void 0 ? false : _a, _b = props.shouldMonitorAccessTokens, shouldMonitorAccessTokens = _b === void 0 ? true : _b, children = props.children; | ||
var options = props.client, _a = props.shouldAttemptLogin, shouldAttemptLogin = _a === void 0 ? false : _a, _b = props.shouldMonitorAccessTokens, shouldMonitorAccessTokens = _b === void 0 ? true : _b, onInitialized = props.onInitialized, children = props.children; | ||
var contextRef = React.useRef(); | ||
@@ -1001,8 +1018,11 @@ if (!contextRef.current) { | ||
var useStore = contextRef.current.useStore; | ||
var _c = useStore(function (state) { return state.methods; }), authorize = _c.authorize, receiveSessionToken = _c.receiveSessionToken, monitorAccessToken = _c.monitorAccessToken, stopMonitoringAccessToken = _c.stopMonitoringAccessToken; | ||
var _c = useStore(function (state) { return state.methods; }), authorize = _c.authorize, loadInitialData = _c.loadInitialData, monitorAccessToken = _c.monitorAccessToken, stopMonitoringAccessToken = _c.stopMonitoringAccessToken; | ||
var isLoggedIn = useStore(function (state) { return state.isLoggedIn; }); | ||
var isCSRFTokenPresent = !!useStore(function (state) { return state.csrfToken; }); | ||
React.useEffect(function () { | ||
receiveSessionToken(); | ||
}, [receiveSessionToken]); | ||
loadInitialData().then(function (data) { | ||
if (onInitialized) | ||
onInitialized(data); | ||
}); | ||
}, [loadInitialData, onInitialized]); | ||
React.useEffect(function () { | ||
@@ -1009,0 +1029,0 @@ if (!isLoggedIn) |
import React from 'react'; | ||
import { UseStore } from 'zustand'; | ||
import { OidcJwtClientOptions, UseOidcJwtClientStore } from './store'; | ||
interface OidcJwtProviderProps { | ||
export interface OidcJwtProviderProps { | ||
client: OidcJwtClientOptions; | ||
shouldAttemptLogin?: boolean; | ||
shouldMonitorAccessTokens?: boolean; | ||
onInitialized?: (data: any | null) => void; | ||
} | ||
@@ -9,0 +10,0 @@ interface OidcJwtContextData { |
@@ -24,2 +24,7 @@ import { UseStore } from 'zustand'; | ||
/** | ||
* Receive session token and return user info | ||
* @returns Promise<Claims | User | null> | ||
*/ | ||
loadInitialData<Claims extends ClaimsBase, User>(): Promise<ClaimsBase | Claims | User | null>; | ||
/** | ||
* Read the session token from the URL. Remove it from the URL if possible. | ||
@@ -29,3 +34,3 @@ * @param redirect If true (the default), redirect to the same page without the token. | ||
*/ | ||
receiveSessionToken(redirect?: boolean): void; | ||
receiveSessionToken<User>(redirect?: boolean): Promise<User | null>; | ||
/** | ||
@@ -41,2 +46,6 @@ * Get a valid access token. If we already have one that's valid, we will not fetch a new one. | ||
getUserInfo<T>(): Promise<T | null>; | ||
/** | ||
* Set user info. | ||
* @returns void | ||
*/ | ||
setUserInfo<T>(userInfo: T): void; | ||
@@ -43,0 +52,0 @@ /** |
{ | ||
"name": "oidc-jwt-client", | ||
"version": "2.0.1-develop.1", | ||
"version": "2.1.0-develop.1", | ||
"description": "Fetch JWTs for API access from oidc-jwt-provider", | ||
@@ -108,5 +108,5 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"react-use": "^15.3.4", | ||
"react-use": "^15.3.8", | ||
"zustand": "^3.1.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
240346
2147
Updatedreact-use@^15.3.8