-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathauth0-context.ts
180 lines (172 loc) · 5.88 KB
/
auth0-context.ts
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
172
173
174
175
176
177
178
179
180
import { createContext } from 'react';
import {
ClearSessionParameters,
LoginWithEmailOptions,
LoginWithOOBOptions,
LoginWithOTPOptions,
LoginWithRecoveryCodeOptions,
LoginWithSMSOptions,
MultifactorChallengeOptions,
PasswordlessWithEmailOptions,
Credentials,
User,
WebAuthorizeOptions,
WebAuthorizeParameters,
PasswordlessWithSMSOptions,
ClearSessionOptions,
PasswordRealmOptions,
ExchangeNativeSocialOptions,
RevokeOptions,
} from '../types';
export interface Auth0ContextInterface<TUser extends User = User>
extends AuthState<TUser> {
/**
* Authorize the user using Auth0 Universal Login. See {@link WebAuth#authorize}
* @param parameters The parameters that are sent to the `/authorize` endpoint.
* @param options Options for customizing the SDK's handling of the authorize call
*/
authorize: (
parameters?: WebAuthorizeParameters,
options?: WebAuthorizeOptions
) => Promise<Credentials | undefined>;
/**
* Cancel any ongoing Universal Login transaction.
* This works only on iOS and not on any other platforms
*/
cancelWebAuth: () => Promise<void>;
/**
* Start the passwordless SMS login flow. See {@link Auth#passwordlessWithSMS}
*/
sendSMSCode: (parameters: PasswordlessWithSMSOptions) => Promise<void>;
/**
* Authorize the user using a SMS code. See {@link Auth#loginWithSMS}
*/
authorizeWithSMS: (
parameters: LoginWithSMSOptions
) => Promise<Credentials | undefined>;
/**
* Start the passwordless email login flow. See {@link Auth#passwordlessWithEmail}
*/
sendEmailCode: (parameters: PasswordlessWithEmailOptions) => Promise<void>;
/**
* Authorize the user using an email code. See {@link Auth#loginWithEmail}
*/
authorizeWithEmail: (
parameters: LoginWithEmailOptions
) => Promise<Credentials | undefined>;
/**
* Send a challenge for multi-factor authentication. See {@link Auth#multifactorChallenge}
*/
sendMultifactorChallenge: (
parameters: MultifactorChallengeOptions
) => Promise<void>;
/**
* Authorize the user using an Out Of Band authentication code. See {@link Auth#loginWithOOB}
*/
authorizeWithOOB: (
parameters: LoginWithOOBOptions
) => Promise<Credentials | undefined>;
/**
* Autohrize the user using a One Time Password code. See {@link Auth#loginWithOTP}.
*/
authorizeWithOTP: (
parameters: LoginWithOTPOptions
) => Promise<Credentials | undefined>;
/**
* Authorize the user using a multi-factor authentication Recovery Code. See {@link Auth#loginWithRecoveryCode}
*/
authorizeWithRecoveryCode: (
parameters: LoginWithRecoveryCodeOptions
) => Promise<Credentials | undefined>;
/**
* Whether the SDK currently holds valid, unexpired credentials.
* @param minTtl The minimum time in seconds that the access token should last before expiration
* @returns `true` if there are valid credentials. Otherwise, `false`.
*/
hasValidCredentials: (minTtl?: number) => Promise<boolean>;
/**
* Clears the user's web session, credentials and logs them out. See {@link WebAuth#clearSession}
* @param parameters Additional parameters to send to the Auth0 logout endpoint.
* @param options Options for configuring the SDK's clear session behaviour.
*/
clearSession: (
parameters?: ClearSessionParameters,
options?: ClearSessionOptions
) => Promise<void>;
/**
* Gets the user's credentials from the native credential store. If credentials have expired, they are automatically refreshed
* by default. See {@link CredentialsManager#getCredentials}
* @param scope The scopes used to get the credentials
* @param minTtl The minimum time in seconds that the access token should last before expiration
* @param parameters Any additional parameters to send in the request to refresh expired credentials.
* @param forceRefresh If `true`, credentials are always refreshed regardless of their expiry, provided a valid refresh token is available.
* @returns
*/
getCredentials: (
scope?: string,
minTtl?: number,
parameters?: Record<string, unknown>,
forceRefresh?: boolean
) => Promise<Credentials | undefined>;
/**
* Clears the user's credentials without clearing their web session and logs them out.
*/
clearCredentials: () => Promise<void>;
/**
* Authorize user with credentials using the Password Realm Grant. See {@link Auth#passwordRealm}
*/
authorizeWithPasswordRealm: (
parameters: PasswordRealmOptions
) => Promise<Credentials | undefined>;
/**
* Authorize user with credentials using the Password Realm Grant. See {@link Auth#passwordRealm}
*/
authorizeWithExchangeNativeSocial: (
parameters: ExchangeNativeSocialOptions
) => Promise<Credentials | undefined>;
/**
*Revokes an issued refresh token. See {@link Auth#revoke}
*/
revokeRefreshToken: (parameters: RevokeOptions) => Promise<void>;
}
export interface AuthState<TUser extends User = User> {
/**
* An object representing the last exception
*/
error: Error | null;
/**
* The user profile as decoded from the ID token after authentication
*/
user: TUser | null;
/**
* A flag that is true until the state knows that a user is either logged in or not
*/
isLoading: boolean;
}
const stub = () => {
throw new Error('No provider was set');
};
const initialContext = {
error: null,
user: null,
isLoading: true,
authorize: stub,
cancelWebAuth: stub,
sendSMSCode: stub,
authorizeWithSMS: stub,
sendEmailCode: stub,
authorizeWithEmail: stub,
sendMultifactorChallenge: stub,
authorizeWithOOB: stub,
authorizeWithOTP: stub,
authorizeWithRecoveryCode: stub,
hasValidCredentials: stub,
clearSession: stub,
getCredentials: stub,
clearCredentials: stub,
authorizeWithPasswordRealm: stub,
authorizeWithExchangeNativeSocial: stub,
revokeRefreshToken: stub,
};
const Auth0Context = createContext<Auth0ContextInterface>(initialContext);
export default Auth0Context;