-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
157 lines (129 loc) · 5.47 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import React, { useState, useEffect, useContext, PropsWithChildren, useRef } from "react";
import createAuth0Client, { PopupLoginOptions, RedirectLoginOptions, LogoutOptions, GetUserOptions, GetTokenSilentlyOptions, GetIdTokenClaimsOptions } from "@auth0/auth0-spa-js";
import { useAsync } from 'react-async-hook';
import Auth0Client from "@auth0/auth0-spa-js/dist/typings/Auth0Client";
const DEFAULT_REDIRECT_CALLBACK = () => window.history.replaceState({}, document.title, window.location.pathname);
export interface Auth0Renders {
clientRef: ClientRef | null;
isAuthenticated: boolean;
loading: boolean;
popupOpen: boolean;
loginWithPopup: (options?: PopupLoginOptions) => void;
loginWithRedirect: (options?: RedirectLoginOptions) => void;
logout: (options?: LogoutOptions) => void;
}
const Auth0Context = React.createContext<Auth0Renders>({
clientRef: null,
isAuthenticated: false,
loading: false,
popupOpen: false,
loginWithPopup: () => { throw Error("not initialized"); },
loginWithRedirect: () => { throw Error("not initialized"); },
logout: () => { throw Error("not initialized"); },
});
export const useAuth0 = () => useContext(Auth0Context);
export interface Auth0ProviderProps {
domain: string;
clientId: string;
redirectUri: string;
scope?: string;
audience?: string;
issuer?: string;
onRedirectCallback?: (appState: any) => void;
}
interface ProviderState {
isInitialized: boolean;
isAuthenticated: boolean;
loading: boolean;
popupOpen: boolean;
}
type ClientRef = React.MutableRefObject<Auth0Client | undefined>;
type ChangeProviderState = (prevState: ProviderState) => ProviderState;
type SetProviderState = (change: ChangeProviderState) => void;
async function initializeClient(props: Auth0ProviderProps, clientRef: ClientRef, setState: SetProviderState) {
const client = await createAuth0Client({
domain: props.domain,
client_id: props.clientId,
redirect_uri: props.redirectUri,
audience: props.audience,
scope: props.scope,
issuer: props.issuer,
});
clientRef.current = client;
if (window.location.search.includes("code=")) {
const { appState } = await client.handleRedirectCallback();
const callback = props.onRedirectCallback || DEFAULT_REDIRECT_CALLBACK;
callback(appState);
}
const isAuthenticated = await client.isAuthenticated();
setState(prevState => ({...prevState, isInitialized: true, isAuthenticated }));
}
function unwrapClientRef(clientRef: ClientRef | null): Auth0Client {
if (!clientRef || !clientRef.current) {
throw new Error("auth0 client has not been initialized");
}
return clientRef.current;
}
async function loginWithPopup(clientRef: ClientRef, setState: SetProviderState, options?: PopupLoginOptions) {
const client = unwrapClientRef(clientRef);
setState(prev => ({...prev, popupOpen: true }));
try {
await client.loginWithPopup(options);
} catch (error) {
console.error(error);
setState(prev => ({...prev, isAuthenticated: false, popupOpen: false }));
}
setState(prev => ({...prev, isAuthenticated: true, popupOpen: false }));
};
async function loginWithRedirect(clientRef: ClientRef, setState: SetProviderState, options?: RedirectLoginOptions) {
const client = unwrapClientRef(clientRef);
try {
await client.loginWithRedirect(options);
} catch (error) {
console.error(error);
setState(prev => ({...prev, isAuthenticated: false, user: null }));
}
};
async function logout(clientRef: ClientRef, setState: SetProviderState, options?: LogoutOptions) {
const client = unwrapClientRef(clientRef);
client.logout(options);
setState(prev => ({...prev, user: null, isAuthenticated: false }));
};
const INITIAL_STATE = { isInitialized: false, isAuthenticated: false, user: null, popupOpen: false, loading: false }
export const Auth0Setup = (props: PropsWithChildren<Auth0ProviderProps>) => {
const [state, setState] = useState<ProviderState>(INITIAL_STATE);
const clientRef = useRef<Auth0Client>();
useEffect(() => {
initializeClient(props, clientRef, setState);
}, []);
return (
<Auth0Context.Provider
value={{
clientRef,
isAuthenticated: state.isAuthenticated,
loading: state.loading,
popupOpen: state.popupOpen,
loginWithPopup: (options?: PopupLoginOptions) => loginWithPopup(clientRef, setState, options),
loginWithRedirect: (options?: RedirectLoginOptions) => loginWithRedirect(clientRef, setState, options),
logout: (options?: LogoutOptions) => logout(clientRef, setState, options),
}}
>
{state.isInitialized ? props.children : null}
</Auth0Context.Provider>
);
};
export function useAuth0User(options?: GetUserOptions) {
const auth0 = useAuth0();
const client = unwrapClientRef(auth0.clientRef);
return useAsync(async () => client.getUser(options), []);
}
export function useAuth0Token(options?: GetTokenSilentlyOptions) {
const auth0 = useAuth0();
const client = unwrapClientRef(auth0.clientRef);
return useAsync(async () => client.getTokenSilently(options), []);
}
export function useAuth0Claims(options?: GetIdTokenClaimsOptions) {
const auth0 = useAuth0();
const client = unwrapClientRef(auth0.clientRef);
return useAsync(async () => client.getIdTokenClaims(options), []);
}