-
Notifications
You must be signed in to change notification settings - Fork 149
/
AuthenticationContext.container.js
125 lines (112 loc) · 3.97 KB
/
AuthenticationContext.container.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React, { useEffect, useCallback, useReducer } from 'react';
import PropTypes from 'prop-types';
import { withRouter, authenticationService, setLogger, OidcRoutes } from '@axa-fr/react-oidc-core';
import { Callback } from '../Callback';
import { addOidcEvents, removeOidcEvents, oidcReducer, login, logout } from './OidcEvents';
import withServices from '../withServices';
export const AuthenticationContext = React.createContext(null);
const propTypes = {
notAuthenticated: PropTypes.elementType,
notAuthorized: PropTypes.elementType,
authenticating: PropTypes.elementType,
callbackComponentOverride: PropTypes.elementType,
configuration: PropTypes.shape({
client_id: PropTypes.string.isRequired,
redirect_uri: PropTypes.string.isRequired,
response_type: PropTypes.string.isRequired,
scope: PropTypes.string.isRequired,
authority: PropTypes.string.isRequired,
silent_redirect_uri: PropTypes.string.isRequired,
automaticSilentRenew: PropTypes.bool.isRequired,
loadUserInfo: PropTypes.bool.isRequired,
triggerAuthFlow: PropTypes.bool.isRequired,
}).isRequired,
isEnabled: PropTypes.bool,
loggerLevel: PropTypes.number,
logger: PropTypes.shape({
info: PropTypes.func.isRequired,
warn: PropTypes.func.isRequired,
error: PropTypes.func.isRequired,
debug: PropTypes.func.isRequired,
}),
};
const defaultProps = {
notAuthenticated: null,
notAuthorized: null,
authenticating: null,
callbackComponentOverride: null,
isEnabled: true,
loggerLevel: 0,
logger: console,
};
export const setDefaultState = ({ configuration, isEnabled }, authenticationServiceInternal) => {
return {
oidcUser: undefined,
userManager: authenticationServiceInternal(configuration),
isLoading: false,
error: '',
isEnabled,
};
};
const withComponentOverrideProps = (Component, customProps) => props => (
<Component callbackComponentOverride={customProps} {...props} />
);
const AuthenticationProviderInt = ({ location, ...otherProps }) => {
const [oidcState, dispatch] = useReducer(
oidcReducer,
setDefaultState(otherProps, authenticationService)
);
useEffect(() => {
setLogger(otherProps.loggerLevel, otherProps.logger);
dispatch({ type: 'ON_LOADING' });
addOidcEvents(oidcState.userManager.events, dispatch, oidcState.userManager.signinSilent);
oidcState.userManager.getUser().then(user => dispatch({ type: 'ON_LOAD_USER', user }));
return () =>
removeOidcEvents(oidcState.userManager.events, dispatch, oidcState.userManager.signinSilent);
}, [otherProps.logger, otherProps.loggerLevel, oidcState.userManager]);
const { oidcUser, isLoading, error, isEnabled } = oidcState;
const {
authenticating,
notAuthenticated,
notAuthorized,
callbackComponentOverride,
configuration,
children,
Callback: CallbackInt,
} = otherProps;
const CallbackComponent = callbackComponentOverride
? withComponentOverrideProps(CallbackInt, callbackComponentOverride)
: CallbackInt;
return (
<AuthenticationContext.Provider
value={{
isLoading,
oidcUser,
error,
authenticating,
isEnabled,
login: useCallback(() => login(oidcState.userManager, dispatch, location)(), [
location,
oidcState.userManager,
]),
logout: useCallback(() => logout(oidcState.userManager, dispatch)(), [
oidcState.userManager,
]),
}}
>
<OidcRoutes
notAuthenticated={notAuthenticated}
notAuthorized={notAuthorized}
callbackComponent={CallbackComponent}
configuration={configuration}
>
{children}
</OidcRoutes>
</AuthenticationContext.Provider>
);
};
const AuthenticationProvider = withRouter(withServices(AuthenticationProviderInt, { Callback }));
AuthenticationProvider.propTypes = propTypes;
AuthenticationProvider.defaultProps = defaultProps;
AuthenticationProvider.displayName = 'AuthenticationProvider';
export default AuthenticationProvider;