@evoke-platform/context 1.5.0-testing.1 → 1.5.0-testing.3

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.
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx } from "react/jsx-runtime";
11
- import { createContext, useCallback, useContext, useMemo } from 'react';
11
+ import { createContext, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
12
12
  const Context = createContext(undefined);
13
13
  Context.displayName = 'AuthenticationContext';
14
14
  function AuthenticationContextProvider(props) {
@@ -64,10 +64,14 @@ function MsalProvider({ msal, authRequest, children }) {
64
64
  return _jsx(Context.Provider, { value: context, children: children });
65
65
  }
66
66
  function OidcProvider({ oidcInstance, authRequest, children }) {
67
- var _a, _b;
67
+ var _a, _b, _c;
68
68
  if (!oidcInstance) {
69
69
  throw new Error('OIDC instance is required for OidcProvider');
70
70
  }
71
+ const userRef = useRef(oidcInstance.user);
72
+ useEffect(() => {
73
+ userRef.current = oidcInstance.user;
74
+ }, [oidcInstance.user]);
71
75
  // The authRequest for react-oidc is formatted slightly differently than msal.
72
76
  const oidcAuthRequest = {
73
77
  scope: (_b = (_a = authRequest.scopes) === null || _a === void 0 ? void 0 : _a.join(' ')) !== null && _b !== void 0 ? _b : 'openid profile email',
@@ -75,14 +79,14 @@ function OidcProvider({ oidcInstance, authRequest, children }) {
75
79
  state: authRequest.state,
76
80
  };
77
81
  const getAccessToken = useCallback(function () {
78
- var _a, _b;
82
+ var _a;
79
83
  return __awaiter(this, void 0, void 0, function* () {
80
84
  try {
81
85
  // With automaticSilentRenew: true, oidc-client-ts will attempt to renew the token in the background before it expires.
82
86
  // However, this is not guaranteed to be perfectly in sync with your API calls. Always check for expiration here and call signinSilent if needed
83
87
  // to ensure you get a valid token on demand.
84
- if (((_a = oidcInstance.user) === null || _a === void 0 ? void 0 : _a.access_token) && !oidcInstance.user.expired) {
85
- return oidcInstance.user.access_token;
88
+ if (((_a = userRef.current) === null || _a === void 0 ? void 0 : _a.access_token) && !userRef.current.expired) {
89
+ return userRef.current.access_token;
86
90
  }
87
91
  // Token is either missing or expired - attempt silent refresh.
88
92
  const user = yield oidcInstance.signinSilent(oidcAuthRequest);
@@ -92,7 +96,7 @@ function OidcProvider({ oidcInstance, authRequest, children }) {
92
96
  oidcInstance.signinRedirect(oidcAuthRequest);
93
97
  return '';
94
98
  }
95
- return ((_b = oidcInstance.user) === null || _b === void 0 ? void 0 : _b.access_token) || '';
99
+ return user.access_token;
96
100
  }
97
101
  catch (error) {
98
102
  console.error('Failed to get access token:', error);
@@ -102,17 +106,17 @@ function OidcProvider({ oidcInstance, authRequest, children }) {
102
106
  return '';
103
107
  }
104
108
  });
105
- }, [oidcInstance, authRequest]);
109
+ }, [oidcInstance.signinSilent, oidcInstance.signinRedirect, authRequest]);
106
110
  const context = useMemo(() => {
107
111
  var _a, _b, _c, _d;
108
- return oidcInstance.isAuthenticated && oidcInstance.user
112
+ return oidcInstance.isAuthenticated && userRef.current
109
113
  ? {
110
114
  account: {
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 : ''}` ||
115
+ id: userRef.current.profile.sub,
116
+ name: (_a = userRef.current.profile.name) !== null && _a !== void 0 ? _a : (`${(_b = userRef.current.profile.given_name) !== null && _b !== void 0 ? _b : ''} ${(_c = userRef.current.profile.family_name) !== null && _c !== void 0 ? _c : ''}` ||
113
117
  undefined),
114
- username: (_d = oidcInstance.user.profile.preferred_username) !== null && _d !== void 0 ? _d : oidcInstance.user.profile.email,
115
- lastLoginTime: oidcInstance.user.profile.lastLoginTime,
118
+ username: (_d = userRef.current.profile.preferred_username) !== null && _d !== void 0 ? _d : userRef.current.profile.email,
119
+ lastLoginTime: userRef.current.profile.lastLoginTime,
116
120
  },
117
121
  logout: () => {
118
122
  oidcInstance.signoutRedirect({
@@ -123,7 +127,13 @@ function OidcProvider({ oidcInstance, authRequest, children }) {
123
127
  getAccessToken,
124
128
  }
125
129
  : undefined;
126
- }, [oidcInstance, getAccessToken]);
130
+ }, [
131
+ // Make sure to update authentication context if the logged-in user changes.
132
+ (_c = userRef.current) === null || _c === void 0 ? void 0 : _c.profile.sub,
133
+ oidcInstance.isAuthenticated,
134
+ oidcInstance.signoutRedirect,
135
+ getAccessToken,
136
+ ]);
127
137
  return _jsx(Context.Provider, { value: context, children: children });
128
138
  }
129
139
  export function useAuthenticationContext() {
@@ -62,6 +62,7 @@ export type Obj = {
62
62
  properties?: Property[];
63
63
  actions?: Action[];
64
64
  formId?: string;
65
+ defaultPanelLayoutId?: string;
65
66
  };
66
67
  export type ObjWithRoot = Obj & {
67
68
  rootObjectId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evoke-platform/context",
3
- "version": "1.5.0-testing.1",
3
+ "version": "1.5.0-testing.3",
4
4
  "description": "Utilities that provide context to Evoke platform widgets",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",