@evoke-platform/context 1.3.1 → 1.3.2-testing.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RedirectRequest } from '@azure/msal-browser';
|
|
2
2
|
import { IMsalContext } from '@azure/msal-react';
|
|
3
3
|
import { ReactNode } from 'react';
|
|
4
|
+
import { AuthContextProps } from 'react-oidc-context';
|
|
4
5
|
export type AuthenticationContext = {
|
|
5
6
|
account: UserAccount;
|
|
6
7
|
logout: VoidFunction;
|
|
@@ -15,6 +16,7 @@ export type UserAccount = {
|
|
|
15
16
|
};
|
|
16
17
|
export type AuthenticationContextProviderProps = {
|
|
17
18
|
msal?: IMsalContext;
|
|
19
|
+
oidcInstance?: AuthContextProps;
|
|
18
20
|
authRequest: AuthenticationRequest;
|
|
19
21
|
children?: ReactNode;
|
|
20
22
|
};
|
|
@@ -9,7 +9,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
11
11
|
import { createContext, useCallback, useContext, useMemo } from 'react';
|
|
12
|
-
import { useAuth } from 'react-oidc-context';
|
|
13
12
|
const Context = createContext(undefined);
|
|
14
13
|
Context.displayName = 'AuthenticationContext';
|
|
15
14
|
function AuthenticationContextProvider(props) {
|
|
@@ -19,8 +18,8 @@ function AuthenticationContextProvider(props) {
|
|
|
19
18
|
return (_jsx(MsalProvider, { msal: msal, authRequest: authRequest, children: children }));
|
|
20
19
|
}
|
|
21
20
|
else {
|
|
22
|
-
const { authRequest, children } = props;
|
|
23
|
-
return _jsx(OidcProvider, { authRequest: authRequest, children: children });
|
|
21
|
+
const { oidcInstance, authRequest, children } = props;
|
|
22
|
+
return (_jsx(OidcProvider, { oidcInstance: oidcInstance, authRequest: authRequest, children: children }));
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
function MsalProvider({ msal, authRequest, children }) {
|
|
@@ -64,15 +63,17 @@ function MsalProvider({ msal, authRequest, children }) {
|
|
|
64
63
|
}, [account, msal, getAccessToken, authRequest]);
|
|
65
64
|
return _jsx(Context.Provider, { value: context, children: children });
|
|
66
65
|
}
|
|
67
|
-
function OidcProvider({ authRequest, children }) {
|
|
66
|
+
function OidcProvider({ oidcInstance, authRequest, children }) {
|
|
68
67
|
var _a, _b;
|
|
68
|
+
if (!oidcInstance) {
|
|
69
|
+
throw new Error('OIDC instance is required for OidcProvider');
|
|
70
|
+
}
|
|
69
71
|
// The authRequest for react-oidc is formatted slightly differently than msal.
|
|
70
72
|
const oidcAuthRequest = {
|
|
71
73
|
scope: (_b = (_a = authRequest.scopes) === null || _a === void 0 ? void 0 : _a.join(' ')) !== null && _b !== void 0 ? _b : 'openid profile email',
|
|
72
74
|
extraQueryParams: authRequest.extraQueryParameters,
|
|
73
75
|
state: authRequest.state,
|
|
74
76
|
};
|
|
75
|
-
const auth = useAuth();
|
|
76
77
|
const getAccessToken = useCallback(function () {
|
|
77
78
|
var _a, _b;
|
|
78
79
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -80,49 +81,49 @@ function OidcProvider({ authRequest, children }) {
|
|
|
80
81
|
// With automaticSilentRenew: true, oidc-client-ts will attempt to renew the token in the background before it expires.
|
|
81
82
|
// However, this is not guaranteed to be perfectly in sync with your API calls. Always check for expiration here and call signinSilent if needed
|
|
82
83
|
// to ensure you get a valid token on demand.
|
|
83
|
-
if (((_a =
|
|
84
|
-
return
|
|
84
|
+
if (((_a = oidcInstance.user) === null || _a === void 0 ? void 0 : _a.access_token) && !oidcInstance.user.expired) {
|
|
85
|
+
return oidcInstance.user.access_token;
|
|
85
86
|
}
|
|
86
87
|
// Token is either missing or expired - attempt silent refresh.
|
|
87
|
-
const user = yield
|
|
88
|
+
const user = yield oidcInstance.signinSilent(oidcAuthRequest);
|
|
88
89
|
// If signinSilent returns null, it means silent login failed
|
|
89
90
|
if (!user) {
|
|
90
91
|
console.log('Silent login failed, redirecting to login');
|
|
91
|
-
|
|
92
|
+
oidcInstance.signinRedirect(oidcAuthRequest);
|
|
92
93
|
return '';
|
|
93
94
|
}
|
|
94
|
-
return ((_b =
|
|
95
|
+
return ((_b = oidcInstance.user) === null || _b === void 0 ? void 0 : _b.access_token) || '';
|
|
95
96
|
}
|
|
96
97
|
catch (error) {
|
|
97
98
|
console.error('Failed to get access token:', error);
|
|
98
99
|
// If silent refresh throws an error (e.g., network failure, missing silent_redirect_uri,
|
|
99
100
|
// invalid session, refresh token expired, or provider returned an error), redirect to login
|
|
100
|
-
|
|
101
|
+
oidcInstance.signinRedirect(oidcAuthRequest);
|
|
101
102
|
return '';
|
|
102
103
|
}
|
|
103
104
|
});
|
|
104
|
-
}, [
|
|
105
|
+
}, [oidcInstance, authRequest]);
|
|
105
106
|
const context = useMemo(() => {
|
|
106
107
|
var _a, _b, _c, _d;
|
|
107
|
-
return
|
|
108
|
+
return oidcInstance.isAuthenticated && oidcInstance.user
|
|
108
109
|
? {
|
|
109
110
|
account: {
|
|
110
|
-
id:
|
|
111
|
-
name: (_a =
|
|
111
|
+
id: oidcInstance.user.profile.sub,
|
|
112
|
+
name: (_a = oidcInstance.user.profile.name) !== null && _a !== void 0 ? _a : (`${(_b = oidcInstance.user.profile.given_name) !== null && _b !== void 0 ? _b : ''} ${(_c = oidcInstance.user.profile.family_name) !== null && _c !== void 0 ? _c : ''}` ||
|
|
112
113
|
undefined),
|
|
113
|
-
username: (_d =
|
|
114
|
-
lastLoginTime:
|
|
114
|
+
username: (_d = oidcInstance.user.profile.preferred_username) !== null && _d !== void 0 ? _d : oidcInstance.user.profile.email,
|
|
115
|
+
lastLoginTime: oidcInstance.user.profile.lastLoginTime,
|
|
115
116
|
},
|
|
116
117
|
logout: () => {
|
|
117
|
-
|
|
118
|
-
// Fusion
|
|
118
|
+
oidcInstance.signoutRedirect({
|
|
119
|
+
// Fusion oidcInstance requires an absolute url.
|
|
119
120
|
post_logout_redirect_uri: `${window.location.origin}/logout?p=${encodeURIComponent(window.location.pathname + window.location.search)}`,
|
|
120
121
|
});
|
|
121
122
|
},
|
|
122
123
|
getAccessToken,
|
|
123
124
|
}
|
|
124
125
|
: undefined;
|
|
125
|
-
}, [
|
|
126
|
+
}, [oidcInstance, getAccessToken]);
|
|
126
127
|
return _jsx(Context.Provider, { value: context, children: children });
|
|
127
128
|
}
|
|
128
129
|
export function useAuthenticationContext() {
|