react-oauth2-code-pkce
Version:
Provider agnostic react package for OAuth2 Authorization Code flow with PKCE
34 lines (33 loc) • 2.89 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInternalConfig = createInternalConfig;
exports.validateConfig = validateConfig;
function stringIsUnset(value) {
const unset = ['', undefined, null];
return unset.includes(value);
}
function createInternalConfig(passedConfig) {
// Set default values for internal config object
const { autoLogin = true, clearURL = true, decodeToken = true, scope = undefined, preLogin = () => null, postLogin = () => null, loginMethod = 'redirect', onRefreshTokenExpire = undefined, storage = 'local', storageKeyPrefix = 'ROCP_', refreshWithScope = true, refreshTokenExpiryStrategy = 'renewable', tokenRequestCredentials = 'same-origin', } = passedConfig;
const config = Object.assign(Object.assign({}, passedConfig), { autoLogin: autoLogin, clearURL: clearURL, decodeToken: decodeToken, scope: scope, preLogin: preLogin, postLogin: postLogin, loginMethod: loginMethod, onRefreshTokenExpire: onRefreshTokenExpire, storage: storage, storageKeyPrefix: storageKeyPrefix, refreshWithScope: refreshWithScope, refreshTokenExpiryStrategy: refreshTokenExpiryStrategy, tokenRequestCredentials: tokenRequestCredentials });
validateConfig(config);
return config;
}
function validateConfig(config) {
if (stringIsUnset(config === null || config === void 0 ? void 0 : config.clientId))
throw Error("'clientId' must be set in the 'AuthConfig' object passed to 'react-oauth2-code-pkce' AuthProvider");
if (stringIsUnset(config === null || config === void 0 ? void 0 : config.authorizationEndpoint))
throw Error("'authorizationEndpoint' must be set in the 'AuthConfig' object passed to 'react-oauth2-code-pkce' AuthProvider");
if (stringIsUnset(config === null || config === void 0 ? void 0 : config.tokenEndpoint))
throw Error("'tokenEndpoint' must be set in the 'AuthConfig' object passed to 'react-oauth2-code-pkce' AuthProvider");
if (stringIsUnset(config === null || config === void 0 ? void 0 : config.redirectUri))
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)
console.warn("The 'extraAuthParams' configuration parameter will be deprecated. You should use " +
"'extraTokenParameters' instead.");
if ((config === null || config === void 0 ? void 0 : config.extraAuthParams) && (config === null || config === void 0 ? void 0 : config.extraTokenParameters))
console.warn("Using both 'extraAuthParams' and 'extraTokenParameters' is not recommended. " +
"They do the same thing, and you should only use 'extraTokenParameters'");
}
;