Skip to content

Commit e05249c

Browse files
committed
Add accessToken to useAuth return value
1 parent eb2e66e commit e05249c

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

src/context.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ import * as React from "react";
44
import { Client } from "./types";
55
import { State, initialState } from "./state";
66

7-
export interface ContextValue extends Client, State {}
7+
export interface ContextValue extends Client, State {
8+
accessToken: string | null;
9+
}
810

9-
export const Context = React.createContext<ContextValue>(
10-
initialState as ContextValue,
11-
);
11+
export const Context = React.createContext<ContextValue>({
12+
...initialState,
13+
signIn: () => Promise.reject(),
14+
signUp: () => Promise.reject(),
15+
getUser: () => null,
16+
getAccessToken: () => Promise.reject(),
17+
signOut: () => Promise.reject(),
18+
switchToOrganization: () => Promise.reject(),
19+
accessToken: null,
20+
});

src/provider.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,28 @@ export function AuthKitProvider(props: AuthKitProviderProps) {
103103
return initialize();
104104
}, [clientId, apiHostname, https, port, redirectUri, refreshBufferInterval]);
105105

106+
const [accessToken, setAccessToken] = React.useState<string | null>(null);
107+
React.useEffect(() => {
108+
const handleAccessTokenChange = (
109+
event: CustomEvent<{ accessToken: string }>,
110+
) => {
111+
setAccessToken(event.detail.accessToken);
112+
};
113+
114+
// authkit-js emits a "authkit:tokenchange" event when the access token is
115+
// refreshed. We want to use this to update the state with the new access
116+
// token so that it is available and up-to-date at render-time.
117+
window.addEventListener("authkit:tokenchange", handleAccessTokenChange);
118+
return () => {
119+
window.removeEventListener(
120+
"authkit:tokenchange",
121+
handleAccessTokenChange,
122+
);
123+
};
124+
}, []);
125+
106126
return (
107-
<Context.Provider value={{ ...client, ...state }}>
127+
<Context.Provider value={{ ...client, ...state, accessToken }}>
108128
{children}
109129
</Context.Provider>
110130
);
@@ -137,3 +157,21 @@ const NOOP_CLIENT: Client = {
137157
switchToOrganization: () => Promise.resolve(),
138158
signOut: async () => {},
139159
};
160+
161+
// TODO: Move this to a global declaration file. Requires some re-configuring of tsconfig.json.
162+
declare global {
163+
interface CustomEventMap {
164+
"authkit:tokenchange": CustomEvent<{ accessToken: string }>;
165+
}
166+
interface Window {
167+
addEventListener<K extends keyof CustomEventMap>(
168+
type: K,
169+
listener: (this: Document, ev: CustomEventMap[K]) => void,
170+
): void;
171+
removeEventListener<K extends keyof CustomEventMap>(
172+
type: K,
173+
listener: (this: Document, ev: CustomEventMap[K]) => void,
174+
): void;
175+
dispatchEvent<K extends keyof CustomEventMap>(ev: CustomEventMap[K]): void;
176+
}
177+
}

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"include": ["src"],
3+
"exclude": ["dist", "node_modules"],
24
"compilerOptions": {
35
"target": "es2016",
46
"lib": ["DOM", "ESNext", "DOM.Iterable"],

0 commit comments

Comments
 (0)