-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathauth0-context.tsx
171 lines (159 loc) · 5.45 KB
/
auth0-context.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
GetTokenSilentlyOptions,
GetTokenWithPopupOptions,
IdToken,
LogoutOptions as SPALogoutOptions,
PopupLoginOptions,
PopupConfigOptions,
RedirectLoginResult,
User,
GetTokenSilentlyVerboseResponse,
RedirectLoginOptions as SPARedirectLoginOptions,
} from '@auth0/auth0-spa-js';
import { createContext } from 'react';
import { AuthState, initialAuthState } from './auth-state';
import { AppState } from './auth0-provider';
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface LogoutOptions extends Omit<SPALogoutOptions, 'onRedirect'> {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface RedirectLoginOptions<TAppState = AppState>
extends Omit<SPARedirectLoginOptions<TAppState>, 'onRedirect'> {}
/**
* Contains the authenticated state and authentication methods provided by the `useAuth0` hook.
*/
export interface Auth0ContextInterface<TUser extends User = User>
extends AuthState<TUser> {
/**
* ```js
* const token = await getAccessTokenSilently(options);
* ```
*
* If there's a valid token stored, return it. Otherwise, opens an
* iframe with the `/authorize` URL using the parameters provided
* as arguments. Random and secure `state` and `nonce` parameters
* will be auto-generated. If the response is successful, results
* will be valid according to their expiration times.
*
* If refresh tokens are used, the token endpoint is called directly with the
* 'refresh_token' grant. If no refresh token is available to make this call,
* the SDK falls back to using an iframe to the '/authorize' URL.
*
* This method may use a web worker to perform the token call if the in-memory
* cache is used.
*
* If an `audience` value is given to this function, the SDK always falls
* back to using an iframe to make the token exchange.
*
* Note that in all cases, falling back to an iframe requires access to
* the `auth0` cookie.
*/
getAccessTokenSilently: {
(
options: GetTokenSilentlyOptions & { detailedResponse: true }
): Promise<GetTokenSilentlyVerboseResponse>;
(options?: GetTokenSilentlyOptions): Promise<string>;
(options: GetTokenSilentlyOptions): Promise<
GetTokenSilentlyVerboseResponse | string
>;
};
/**
* ```js
* const token = await getTokenWithPopup(options, config);
* ```
*
* Get an access token interactively.
*
* Opens a popup with the `/authorize` URL using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated. If the response is successful,
* results will be valid according to their expiration times.
*/
getAccessTokenWithPopup: (
options?: GetTokenWithPopupOptions,
config?: PopupConfigOptions
) => Promise<string | undefined>;
/**
* ```js
* const claims = await getIdTokenClaims();
* ```
*
* Returns all claims from the id_token if available.
*/
getIdTokenClaims: () => Promise<IdToken | undefined>;
/**
* ```js
* await loginWithRedirect(options);
* ```
*
* Performs a redirect to `/authorize` using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated.
*/
loginWithRedirect: (
options?: RedirectLoginOptions<AppState>
) => Promise<void>;
/**
* ```js
* await loginWithPopup(options, config);
* ```
*
* Opens a popup with the `/authorize` URL using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated. If the response is successful,
* results will be valid according to their expiration times.
*
* IMPORTANT: This method has to be called from an event handler
* that was started by the user like a button click, for example,
* otherwise the popup will be blocked in most browsers.
*/
loginWithPopup: (
options?: PopupLoginOptions,
config?: PopupConfigOptions
) => Promise<void>;
/**
* ```js
* auth0.logout({ returnTo: window.location.origin });
* ```
*
* Clears the application session and performs a redirect to `/v2/logout`, using
* the parameters provided as arguments, to clear the Auth0 session.
* If the `logoutParams.federated` option is specified, it also clears the Identity Provider session.
* [Read more about how Logout works at Auth0](https://auth0.com/docs/logout).
*/
logout: (options?: LogoutOptions) => void;
/**
* After the browser redirects back to the callback page,
* call `handleRedirectCallback` to handle success and error
* responses from Auth0. If the response is successful, results
* will be valid according to their expiration times.
*
* @param url The URL to that should be used to retrieve the `state` and `code` values. Defaults to `window.location.href` if not given.
*/
handleRedirectCallback: (url?: string) => Promise<RedirectLoginResult>;
}
/**
* @ignore
*/
const stub = (): never => {
throw new Error('You forgot to wrap your component in <Auth0Provider>.');
};
/**
* @ignore
*/
export const initialContext = {
...initialAuthState,
buildAuthorizeUrl: stub,
buildLogoutUrl: stub,
getAccessTokenSilently: stub,
getAccessTokenWithPopup: stub,
getIdTokenClaims: stub,
loginWithRedirect: stub,
loginWithPopup: stub,
logout: stub,
handleRedirectCallback: stub,
};
/**
* The Auth0 Context
*/
const Auth0Context = createContext<Auth0ContextInterface>(initialContext);
export default Auth0Context;