@evoke-platform/context 1.3.0-dev.4 → 1.3.0-dev.6
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.
|
@@ -9,11 +9,25 @@ 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';
|
|
12
13
|
const Context = createContext(undefined);
|
|
13
14
|
Context.displayName = 'AuthenticationContext';
|
|
14
15
|
function AuthenticationContextProvider(props) {
|
|
16
|
+
// Auto-detect provider type based on presence of msal prop
|
|
17
|
+
if (props.msal) {
|
|
18
|
+
const { msal, authRequest, children } = props;
|
|
19
|
+
return (_jsx(MsalProvider, { msal: msal, authRequest: authRequest, children: children }));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
const { authRequest, children } = props;
|
|
23
|
+
return _jsx(OidcProvider, { authRequest: authRequest, children: children });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function MsalProvider({ msal, authRequest, children }) {
|
|
15
27
|
var _a;
|
|
16
|
-
|
|
28
|
+
if (!msal) {
|
|
29
|
+
throw new Error('MSAL instance is required for MsalProvider');
|
|
30
|
+
}
|
|
17
31
|
const account = (_a = msal.instance.getActiveAccount()) !== null && _a !== void 0 ? _a : msal.instance.getAllAccounts()[0];
|
|
18
32
|
const getAccessToken = useCallback(function () {
|
|
19
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -26,7 +40,7 @@ function AuthenticationContextProvider(props) {
|
|
|
26
40
|
return '';
|
|
27
41
|
}
|
|
28
42
|
});
|
|
29
|
-
}, [msal, authRequest]);
|
|
43
|
+
}, [msal, authRequest, account]);
|
|
30
44
|
const context = useMemo(() => account
|
|
31
45
|
? {
|
|
32
46
|
account: { id: account.localAccountId, name: account.name },
|
|
@@ -38,7 +52,58 @@ function AuthenticationContextProvider(props) {
|
|
|
38
52
|
},
|
|
39
53
|
getAccessToken,
|
|
40
54
|
}
|
|
41
|
-
: undefined, [account, msal, getAccessToken]);
|
|
55
|
+
: undefined, [account, msal, getAccessToken, authRequest]);
|
|
56
|
+
return _jsx(Context.Provider, { value: context, children: children });
|
|
57
|
+
}
|
|
58
|
+
function OidcProvider({ authRequest, children }) {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
// The authRequest for react-oidc is formatted slightly differently than msal.
|
|
61
|
+
const oidcAuthRequest = {
|
|
62
|
+
scope: (_b = (_a = authRequest.scopes) === null || _a === void 0 ? void 0 : _a.join(' ')) !== null && _b !== void 0 ? _b : 'openid profile email',
|
|
63
|
+
extraQueryParams: authRequest.extraQueryParameters,
|
|
64
|
+
state: authRequest.state,
|
|
65
|
+
};
|
|
66
|
+
const auth = useAuth();
|
|
67
|
+
const getAccessToken = useCallback(function () {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
try {
|
|
71
|
+
// With automaticSilentRenew: true, oidc-client-ts will attempt to renew the token in the background before it expires.
|
|
72
|
+
// However, this is not guaranteed to be perfectly in sync with your API calls. Always check for expiration here and call signinSilent if needed
|
|
73
|
+
// to ensure you get a valid token on demand.
|
|
74
|
+
if (((_a = auth.user) === null || _a === void 0 ? void 0 : _a.access_token) && !auth.user.expired) {
|
|
75
|
+
return auth.user.access_token;
|
|
76
|
+
}
|
|
77
|
+
// Token is either missing or expired - attempt silent refresh.
|
|
78
|
+
const user = yield auth.signinSilent(oidcAuthRequest);
|
|
79
|
+
// If signinSilent returns null, it means silent login failed
|
|
80
|
+
if (!user) {
|
|
81
|
+
console.log('Silent login failed, redirecting to login');
|
|
82
|
+
auth.signinRedirect(oidcAuthRequest);
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
return ((_b = auth.user) === null || _b === void 0 ? void 0 : _b.access_token) || '';
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
console.error('Failed to get access token:', error);
|
|
89
|
+
// If silent refresh throws an error (e.g., network failure, missing silent_redirect_uri,
|
|
90
|
+
// invalid session, refresh token expired, or provider returned an error), redirect to login
|
|
91
|
+
auth.signinRedirect(oidcAuthRequest);
|
|
92
|
+
return '';
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}, [auth, authRequest]);
|
|
96
|
+
const context = useMemo(() => auth.isAuthenticated && auth.user
|
|
97
|
+
? {
|
|
98
|
+
account: { id: auth.user.profile.sub, name: auth.user.profile.name },
|
|
99
|
+
logout: () => {
|
|
100
|
+
auth.signoutRedirect({
|
|
101
|
+
post_logout_redirect_uri: `/logout?p=${encodeURIComponent(window.location.pathname + window.location.search)}`,
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
getAccessToken,
|
|
105
|
+
}
|
|
106
|
+
: undefined, [auth, getAccessToken]);
|
|
42
107
|
return _jsx(Context.Provider, { value: context, children: children });
|
|
43
108
|
}
|
|
44
109
|
export function useAuthenticationContext() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evoke-platform/context",
|
|
3
|
-
"version": "1.3.0-dev.
|
|
3
|
+
"version": "1.3.0-dev.6",
|
|
4
4
|
"description": "Utilities that provide context to Evoke platform widgets",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -56,6 +56,7 @@
|
|
|
56
56
|
"msw": "^1.3.1",
|
|
57
57
|
"react": "^18.2.0",
|
|
58
58
|
"react-dom": "^18.3.1",
|
|
59
|
+
"react-oidc-context": "^2.4.0",
|
|
59
60
|
"react-router-dom": "^6.16.0",
|
|
60
61
|
"sinon": "^18.0.0",
|
|
61
62
|
"typescript": "^5.3.3"
|
|
@@ -64,12 +65,14 @@
|
|
|
64
65
|
"@azure/msal-browser": ">=2",
|
|
65
66
|
"@azure/msal-react": ">=1",
|
|
66
67
|
"react": ">=18",
|
|
68
|
+
"react-oidc-context": ">=2",
|
|
67
69
|
"react-router-dom": ">=6"
|
|
68
70
|
},
|
|
69
71
|
"dependencies": {
|
|
70
72
|
"@isaacs/ttlcache": "^1.4.1",
|
|
71
73
|
"@microsoft/signalr": "^7.0.12",
|
|
72
74
|
"axios": "^1.7.9",
|
|
75
|
+
"oidc-client-ts": "^3.3.0",
|
|
73
76
|
"uuid": "^9.0.1"
|
|
74
77
|
}
|
|
75
78
|
}
|