diff --git a/src/index.ts b/src/index.ts index 44e7120..1ddf44c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ export { useAuth } from "./hook"; export { AuthKitProvider } from "./provider"; export { getClaims } from "@workos-inc/authkit-js"; +export type { Impersonator } from "./state"; diff --git a/src/provider.tsx b/src/provider.tsx index c9a4fe4..375fdd4 100644 --- a/src/provider.tsx +++ b/src/provider.tsx @@ -35,20 +35,26 @@ export function AuthKitProvider(props: AuthKitProviderProps) { const handleRefresh = React.useCallback( (response: OnRefreshResponse) => { - const { user, accessToken, organizationId } = response; + const { + user, + accessToken, + organizationId = null, + impersonator = null, + } = response; const { role = null, permissions = [], - feature_flags = [], + feature_flags: featureFlags = [], } = getClaims(accessToken); setState((prev) => { const next = { ...prev, user, - organizationId: organizationId ?? null, + organizationId, role, permissions, - featureFlags: feature_flags, + featureFlags, + impersonator, }; return isEquivalentWorkOSSession(prev, next) ? prev : next; }); @@ -114,7 +120,9 @@ function isEquivalentWorkOSSession( a.permissions.length === b.permissions.length && a.permissions.every((perm, i) => perm === b.permissions[i]) && a.featureFlags.length === b.featureFlags.length && - a.featureFlags.every((flag, i) => flag === b.featureFlags[i]) + a.featureFlags.every((flag, i) => flag === b.featureFlags[i]) && + a.impersonator?.email === b.impersonator?.email && + a.impersonator?.reason === b.impersonator?.reason ); } diff --git a/src/state.ts b/src/state.ts index 1c0f403..22c186f 100644 --- a/src/state.ts +++ b/src/state.ts @@ -1,5 +1,10 @@ import { User } from "@workos-inc/authkit-js"; +export interface Impersonator { + email: string; + reason: string | null; +} + export interface State { isLoading: boolean; user: User | null; @@ -7,6 +12,7 @@ export interface State { organizationId: string | null; permissions: string[]; featureFlags: string[]; + impersonator: Impersonator | null; } export const initialState: State = { @@ -16,4 +22,5 @@ export const initialState: State = { organizationId: null, permissions: [], featureFlags: [], + impersonator: null, };