oidc-jwt-client
Advanced tools
Comparing version 2.1.0-develop.1 to 2.1.0-develop.2
@@ -17,2 +17,3 @@ import { AsyncState } from 'react-use/lib/useAsync'; | ||
}; | ||
export { useAuthControls, useAuthUserInfo, useAuthAccessClaims, useAuthIsLoggedIn, useAuthAccessToken, useAuthSessionInfo, }; | ||
declare function useAuthInitialized(): any; | ||
export { useAuthControls, useAuthUserInfo, useAuthAccessClaims, useAuthIsLoggedIn, useAuthAccessToken, useAuthSessionInfo, useAuthInitialized, }; |
@@ -795,3 +795,9 @@ import React, { useReducer, useRef, useEffect, useLayoutEffect, useContext, useCallback, useState } from 'react'; | ||
isLoggedIn: isLoggedInPersistentValue ? parseJson(isLoggedInPersistentValue) : false, | ||
initializedData: undefined, | ||
methods: { | ||
setInitializedData: function (initializedData) { | ||
set({ | ||
initializedData: initializedData, | ||
}); | ||
}, | ||
setIsLoggedIn: function (isLoggedIn) { | ||
@@ -836,33 +842,36 @@ if (!isSSR) | ||
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; }); | ||
} | ||
var _a = get().methods, receiveSessionToken = _a.receiveSessionToken, getAccessToken = _a.getAccessToken, getInitialUserInfo = _a.getInitialUserInfo, setInitializedData = _a.setInitializedData; | ||
var token = receiveSessionToken(); | ||
return getInitialUserInfo(token, true).then(function () { return getAccessToken().then(function (info) { | ||
var _a; | ||
setInitializedData((_a = info === null || info === void 0 ? void 0 : info.claims) !== null && _a !== void 0 ? _a : null); | ||
}); }); | ||
}, | ||
removeTokenFromUrl: function (data, redirect) { | ||
if (redirect === void 0) { redirect = true; } | ||
if (redirect) { | ||
var urlWithoutToken = stripTokenFromUrl(window.location.href).replace(/\?$/, '').replace(/#\.$/, ''); | ||
window.history.replaceState({}, '', urlWithoutToken); | ||
return data; | ||
}); | ||
} | ||
return null; | ||
}, | ||
receiveSessionToken: function (redirect) { | ||
var _this = this; | ||
getInitialUserInfo: function (token, redirect) { | ||
if (redirect === void 0) { redirect = true; } | ||
var _a = get().methods, setSessionToken = _a.setSessionToken, getUserInfo = _a.getUserInfo; | ||
var _b = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || [], token = _b[1]; | ||
var _a = get().methods, getUserInfo = _a.getUserInfo, removeTokenFromUrl = _a.removeTokenFromUrl; | ||
var getUserInfoPromise = getUserInfo(); | ||
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) | ||
.catch(userInfoHandler); | ||
.then(function (data) { return removeTokenFromUrl(data, redirect); }) | ||
.catch(function (data) { return removeTokenFromUrl(data, redirect); }); | ||
}, | ||
receiveSessionToken: function () { | ||
var setSessionToken = get().methods.setSessionToken; | ||
var _a = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || [], token = _a[1]; | ||
if (!token) | ||
return null; | ||
setSessionToken(token); | ||
return token; | ||
}, | ||
validateAccessTokenCache: function (cache, currentAccessTokenCache) { | ||
@@ -1001,3 +1010,3 @@ var _a = get(), accessTokenCache = _a.accessTokenCache, getAccessToken = _a.methods.getAccessToken; | ||
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, onInitialized = props.onInitialized, 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, children = props.children; | ||
var contextRef = useRef(); | ||
@@ -1014,7 +1023,4 @@ if (!contextRef.current) { | ||
useEffect(function () { | ||
loadInitialData().then(function (data) { | ||
if (onInitialized) | ||
onInitialized(data); | ||
}); | ||
}, [loadInitialData, onInitialized]); | ||
loadInitialData(); | ||
}, [loadInitialData]); | ||
useEffect(function () { | ||
@@ -1155,4 +1161,8 @@ if (!isLoggedIn) | ||
} | ||
function useAuthInitialized() { | ||
var useStore = useOidcJwtContext().useStore; | ||
return useStore(function (state) { return state.initializedData; }); | ||
} | ||
export { OidcJwtProvider, useAuthAccessClaims, useAuthAccessToken, useAuthControls, useAuthIsLoggedIn, useAuthSessionInfo, useAuthUserInfo, useOidcJwtContext }; | ||
export { OidcJwtProvider, useAuthAccessClaims, useAuthAccessToken, useAuthControls, useAuthInitialized, useAuthIsLoggedIn, useAuthSessionInfo, useAuthUserInfo, useOidcJwtContext }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -803,3 +803,9 @@ 'use strict'; | ||
isLoggedIn: isLoggedInPersistentValue ? parseJson(isLoggedInPersistentValue) : false, | ||
initializedData: undefined, | ||
methods: { | ||
setInitializedData: function (initializedData) { | ||
set({ | ||
initializedData: initializedData, | ||
}); | ||
}, | ||
setIsLoggedIn: function (isLoggedIn) { | ||
@@ -844,33 +850,36 @@ if (!isSSR) | ||
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; }); | ||
} | ||
var _a = get().methods, receiveSessionToken = _a.receiveSessionToken, getAccessToken = _a.getAccessToken, getInitialUserInfo = _a.getInitialUserInfo, setInitializedData = _a.setInitializedData; | ||
var token = receiveSessionToken(); | ||
return getInitialUserInfo(token, true).then(function () { return getAccessToken().then(function (info) { | ||
var _a; | ||
setInitializedData((_a = info === null || info === void 0 ? void 0 : info.claims) !== null && _a !== void 0 ? _a : null); | ||
}); }); | ||
}, | ||
removeTokenFromUrl: function (data, redirect) { | ||
if (redirect === void 0) { redirect = true; } | ||
if (redirect) { | ||
var urlWithoutToken = stripTokenFromUrl(window.location.href).replace(/\?$/, '').replace(/#\.$/, ''); | ||
window.history.replaceState({}, '', urlWithoutToken); | ||
return data; | ||
}); | ||
} | ||
return null; | ||
}, | ||
receiveSessionToken: function (redirect) { | ||
var _this = this; | ||
getInitialUserInfo: function (token, redirect) { | ||
if (redirect === void 0) { redirect = true; } | ||
var _a = get().methods, setSessionToken = _a.setSessionToken, getUserInfo = _a.getUserInfo; | ||
var _b = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || [], token = _b[1]; | ||
var _a = get().methods, getUserInfo = _a.getUserInfo, removeTokenFromUrl = _a.removeTokenFromUrl; | ||
var getUserInfoPromise = getUserInfo(); | ||
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) | ||
.catch(userInfoHandler); | ||
.then(function (data) { return removeTokenFromUrl(data, redirect); }) | ||
.catch(function (data) { return removeTokenFromUrl(data, redirect); }); | ||
}, | ||
receiveSessionToken: function () { | ||
var setSessionToken = get().methods.setSessionToken; | ||
var _a = (!isSSR && window.location.search.match(/[?&]token=([^&]+)/)) || [], token = _a[1]; | ||
if (!token) | ||
return null; | ||
setSessionToken(token); | ||
return token; | ||
}, | ||
validateAccessTokenCache: function (cache, currentAccessTokenCache) { | ||
@@ -1009,3 +1018,3 @@ var _a = get(), accessTokenCache = _a.accessTokenCache, getAccessToken = _a.methods.getAccessToken; | ||
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, onInitialized = props.onInitialized, 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, children = props.children; | ||
var contextRef = React.useRef(); | ||
@@ -1022,7 +1031,4 @@ if (!contextRef.current) { | ||
React.useEffect(function () { | ||
loadInitialData().then(function (data) { | ||
if (onInitialized) | ||
onInitialized(data); | ||
}); | ||
}, [loadInitialData, onInitialized]); | ||
loadInitialData(); | ||
}, [loadInitialData]); | ||
React.useEffect(function () { | ||
@@ -1163,2 +1169,6 @@ if (!isLoggedIn) | ||
} | ||
function useAuthInitialized() { | ||
var useStore = useOidcJwtContext().useStore; | ||
return useStore(function (state) { return state.initializedData; }); | ||
} | ||
@@ -1169,2 +1179,3 @@ exports.OidcJwtProvider = OidcJwtProvider; | ||
exports.useAuthControls = useAuthControls; | ||
exports.useAuthInitialized = useAuthInitialized; | ||
exports.useAuthIsLoggedIn = useAuthIsLoggedIn; | ||
@@ -1171,0 +1182,0 @@ exports.useAuthSessionInfo = useAuthSessionInfo; |
@@ -8,3 +8,2 @@ import React from 'react'; | ||
shouldMonitorAccessTokens?: boolean; | ||
onInitialized?: (data: any | null) => void; | ||
} | ||
@@ -11,0 +10,0 @@ interface OidcJwtContextData { |
@@ -23,2 +23,3 @@ import { UseStore } from 'zustand'; | ||
setIsLoggedIn(loggedIn: boolean): void; | ||
setInitializedData<Claims>(initializedData: Claims | null): void; | ||
/** | ||
@@ -28,9 +29,20 @@ * Receive session token and return user info | ||
*/ | ||
loadInitialData<Claims extends ClaimsBase, User>(): Promise<ClaimsBase | Claims | User | null>; | ||
loadInitialData<Claims extends ClaimsBase, User>(): Promise<void>; | ||
/** | ||
* Try to remove token from url if redirect is true | ||
* @param redirect If true (the default), redirect to the same page without the token. | ||
* @returns User | null | ||
*/ | ||
removeTokenFromUrl<User>(data: User | null, redirect?: boolean): User | null; | ||
/** | ||
* Get initial user info promise | ||
* @param redirect If true (the default), redirect to the same page without the token. | ||
* @returns Promise<User | null> | ||
*/ | ||
getInitialUserInfo<User>(token: string | null, redirect?: boolean): Promise<User | null>; | ||
/** | ||
* Read the session token from the URL. Remove it from the URL if possible. | ||
* @param redirect If true (the default), redirect to the same page without the token. | ||
* @returns Whether a redirect is taking place. | ||
*/ | ||
receiveSessionToken<User>(redirect?: boolean): Promise<User | null>; | ||
receiveSessionToken(): string | null; | ||
/** | ||
@@ -98,2 +110,3 @@ * Get a valid access token. If we already have one that's valid, we will not fetch a new one. | ||
userInfo: any; | ||
initializedData?: any | null; | ||
isLoggedIn: boolean; | ||
@@ -100,0 +113,0 @@ methods: StoreMethods; |
{ | ||
"name": "oidc-jwt-client", | ||
"version": "2.1.0-develop.1", | ||
"version": "2.1.0-develop.2", | ||
"description": "Fetch JWTs for API access from oidc-jwt-provider", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -84,1 +84,11 @@ # oidc-jwt-client | ||
``` | ||
### Get the initialized data | ||
Checks when the loadInitialData function load on is done executing and will return with the claims or null, | ||
depending on if you were logged in or logged out on initialization. | ||
Default is undefined. | ||
```javascript | ||
const initializedData = useAuthInitialized(); | ||
console.log('This is the initialized data: ', initializedData) | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
242000
2181
94