react-oauth2-code-pkce
Advanced tools
Comparing version 1.12.3 to 1.13.0
@@ -45,9 +45,13 @@ "use strict"; | ||
const AuthProvider = ({ authConfig, children }) => { | ||
const [refreshToken, setRefreshToken] = (0, Hooks_1.default)('ROCP_refreshToken', undefined); | ||
const [refreshTokenExpire, setRefreshTokenExpire] = (0, Hooks_1.default)('ROCP_refreshTokenExpire', (0, timeUtils_1.epochAtSecondsFromNow)(2 * timeUtils_1.FALLBACK_EXPIRE_TIME)); | ||
const [token, setToken] = (0, Hooks_1.default)('ROCP_token', ''); | ||
const [tokenExpire, setTokenExpire] = (0, Hooks_1.default)('ROCP_tokenExpire', (0, timeUtils_1.epochAtSecondsFromNow)(timeUtils_1.FALLBACK_EXPIRE_TIME)); | ||
const [idToken, setIdToken] = (0, Hooks_1.default)('ROCP_idToken', undefined); | ||
const [loginInProgress, setLoginInProgress] = (0, Hooks_1.default)('ROCP_loginInProgress', false); | ||
const [refreshInProgress, setRefreshInProgress] = (0, Hooks_1.default)('ROCP_refreshInProgress', false); | ||
// Set default values for internal config object | ||
const { autoLogin = true, clearURL = true, decodeToken = true, scope = '', preLogin = () => null, postLogin = () => null, onRefreshTokenExpire = undefined, storage = 'local', } = authConfig; | ||
const config = Object.assign(Object.assign({}, authConfig), { autoLogin: autoLogin, clearURL: clearURL, decodeToken: decodeToken, scope: scope, preLogin: preLogin, postLogin: postLogin, onRefreshTokenExpire: onRefreshTokenExpire, storage: storage }); | ||
(0, validateAuthConfig_1.validateAuthConfig)(config); | ||
const [refreshToken, setRefreshToken] = (0, Hooks_1.default)('ROCP_refreshToken', undefined, config.storage); | ||
const [refreshTokenExpire, setRefreshTokenExpire] = (0, Hooks_1.default)('ROCP_refreshTokenExpire', (0, timeUtils_1.epochAtSecondsFromNow)(2 * timeUtils_1.FALLBACK_EXPIRE_TIME), config.storage); | ||
const [token, setToken] = (0, Hooks_1.default)('ROCP_token', '', config.storage); | ||
const [tokenExpire, setTokenExpire] = (0, Hooks_1.default)('ROCP_tokenExpire', (0, timeUtils_1.epochAtSecondsFromNow)(timeUtils_1.FALLBACK_EXPIRE_TIME), config.storage); | ||
const [idToken, setIdToken] = (0, Hooks_1.default)('ROCP_idToken', undefined, config.storage); | ||
const [loginInProgress, setLoginInProgress] = (0, Hooks_1.default)('ROCP_loginInProgress', false, config.storage); | ||
const [refreshInProgress, setRefreshInProgress] = (0, Hooks_1.default)('ROCP_refreshInProgress', false, config.storage); | ||
const [tokenData, setTokenData] = (0, react_1.useState)(); | ||
@@ -57,6 +61,2 @@ const [idTokenData, setIdTokenData] = (0, react_1.useState)(); | ||
let interval; | ||
// Set default values for internal config object | ||
const { autoLogin = true, clearURL = true, decodeToken = true, scope = '', preLogin = () => null, postLogin = () => null, onRefreshTokenExpire = undefined, } = authConfig; | ||
const config = Object.assign(Object.assign({}, authConfig), { autoLogin: autoLogin, clearURL: clearURL, decodeToken: decodeToken, scope: scope, preLogin: preLogin, postLogin: postLogin, onRefreshTokenExpire: onRefreshTokenExpire }); | ||
(0, validateAuthConfig_1.validateAuthConfig)(config); | ||
function clearStorage() { | ||
@@ -63,0 +63,0 @@ setRefreshToken(undefined); |
@@ -19,3 +19,3 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Create and store a random string in localStorage, used as the 'code_verifier' | ||
// Create and store a random string in sessionStorage, used as the 'code_verifier' | ||
const codeVerifier = (0, pkceUtils_1.generateRandomString)(96); | ||
@@ -22,0 +22,0 @@ sessionStorage.setItem(codeVerifierStorageKey, codeVerifier); |
@@ -1,2 +0,2 @@ | ||
declare function useLocalStorage<T>(key: string, initialValue: T): [T, (v: T) => void]; | ||
export default useLocalStorage; | ||
declare function useBrowserStorage<T>(key: string, initialValue: T, type: 'session' | 'local'): [T, (v: T) => void]; | ||
export default useBrowserStorage; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_1 = require("react"); | ||
function useLocalStorage(key, initialValue) { | ||
function useBrowserStorage(key, initialValue, type) { | ||
const storage = type === 'session' ? sessionStorage : localStorage; | ||
const [storedValue, setStoredValue] = (0, react_1.useState)(() => { | ||
const item = localStorage.getItem(key); | ||
const item = storage.getItem(key); | ||
try { | ||
@@ -19,3 +20,3 @@ return item ? JSON.parse(item) : initialValue; | ||
setStoredValue(value); | ||
localStorage.removeItem(key); | ||
storage.removeItem(key); | ||
return; | ||
@@ -26,3 +27,3 @@ } | ||
setStoredValue(valueToStore); | ||
localStorage.setItem(key, JSON.stringify(valueToStore)); | ||
storage.setItem(key, JSON.stringify(valueToStore)); | ||
} | ||
@@ -36,3 +37,3 @@ catch (error) { | ||
var _a; | ||
if (event.storageArea === localStorage && event.key === key) { | ||
if (event.storageArea === storage && event.key === key) { | ||
setStoredValue(JSON.parse((_a = event.newValue) !== null && _a !== void 0 ? _a : '')); | ||
@@ -46,2 +47,2 @@ } | ||
} | ||
exports.default = useLocalStorage; | ||
exports.default = useBrowserStorage; |
@@ -72,2 +72,3 @@ import { ReactNode } from 'react'; | ||
refreshTokenExpiresIn?: number; | ||
storage?: 'session' | 'local'; | ||
}; | ||
@@ -106,3 +107,4 @@ export type TRefreshTokenExpiredEvent = { | ||
refreshTokenExpiresIn?: number; | ||
storage: 'session' | 'local'; | ||
}; | ||
export {}; |
@@ -17,2 +17,4 @@ "use strict"; | ||
throw Error("'redirectUri' must be set in the 'AuthConfig' object passed to 'react-oauth2-code-pkce' AuthProvider"); | ||
if (!['session', 'local'].includes(config.storage)) | ||
throw Error("'storage' must be one of ('session', 'local')"); | ||
if (config === null || config === void 0 ? void 0 : config.extraAuthParams) | ||
@@ -19,0 +21,0 @@ console.warn("The 'extraAuthParams' configuration parameter will be deprecated. You should use " + |
{ | ||
"name": "react-oauth2-code-pkce", | ||
"version": "1.12.3", | ||
"version": "1.13.0", | ||
"description": "Provider agnostic react package for OAuth2 Authorization Code flow with PKCE", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -144,2 +144,7 @@ # react-oauth2-code-pkce · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/soofstad/react-oauth2-pkce/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/react-oauth2-code-pkce)](https://www.npmjs.com/package/react-oauth2-code-pkce) ![CI](https://github.com/soofstad/react-oauth2-pkce/actions/workflows/tests.yaml/badge.svg) | ||
autoLogin?: boolean // default: true | ||
// Store login state in 'localStorage' or 'sessionStorage' | ||
// If set to 'session', no login state is persisted by 'react-oauth2-code-pkce` when the browser closes. | ||
// NOTE: Many authentication servers will keep the client logged in by cookies. You should therefore use | ||
// the 'logout()'-function to properly logout the client. Or configure your server not to issue cookies. | ||
storage?: 'local' | 'session' // default: 'local' | ||
// Set to false if you need to access the urlParameters sent back from the login server. | ||
@@ -146,0 +151,0 @@ clearURL?: boolean // default: true |
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
43841
720
191