-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathauthConfig.ts
70 lines (66 loc) · 2.55 KB
/
authConfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { TAuthConfig, TInternalConfig } from './types'
function stringIsUnset(value: string | null | undefined) {
const unset = ['', undefined, null]
return unset.includes(value)
}
export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig {
// 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',
}: TAuthConfig = passedConfig
const config: TInternalConfig = {
...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
}
export function validateConfig(config: TInternalConfig) {
if (stringIsUnset(config?.clientId))
throw Error("'clientId' must be set in the 'AuthConfig' object passed to 'react-oauth2-code-pkce' AuthProvider")
if (stringIsUnset(config?.authorizationEndpoint))
throw Error(
"'authorizationEndpoint' must be set in the 'AuthConfig' object passed to 'react-oauth2-code-pkce' AuthProvider"
)
if (stringIsUnset(config?.tokenEndpoint))
throw Error(
"'tokenEndpoint' must be set in the 'AuthConfig' object passed to 'react-oauth2-code-pkce' AuthProvider"
)
if (stringIsUnset(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?.extraAuthParams)
console.warn(
"The 'extraAuthParams' configuration parameter will be deprecated. You should use " +
"'extraTokenParameters' instead."
)
if (config?.extraAuthParams && config?.extraTokenParameters)
console.warn(
"Using both 'extraAuthParams' and 'extraTokenParameters' is not recommended. " +
"They do the same thing, and you should only use 'extraTokenParameters'"
)
}