@jsb188/auth-web 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +29 -0
- package/src/hooks/use-auth-popup.ts +167 -0
- package/src/hooks/use-google-one-tap.ts +138 -0
- package/src/hooks/use-oauth.tsx +381 -0
- package/src/main.ts +21 -0
- package/src/modules/AssignRequiredAuth.tsx +274 -0
- package/src/modules/AuthFormUI.tsx +45 -0
- package/src/modules/AuthForms-SSO.tsx +209 -0
- package/src/modules/AuthForms.tsx +914 -0
- package/src/modules/AuthGate.tsx +123 -0
- package/src/modules/ConfirmPublicFormEmailCode.tsx +144 -0
- package/src/modules/InlineAuthForm.tsx +459 -0
- package/src/modules/ManagedSessions.tsx +115 -0
- package/src/modules/OtherAuthButtons.tsx +118 -0
- package/src/modules/_helpers.ts +200 -0
- package/src/modules/schemas.ts +177 -0
- package/src/schemas/account.ts +114 -0
- package/src/schemas/address.ts +133 -0
- package/src/schemas/index.ts +2 -0
- package/src/ui/ManagedSessionUI.tsx +95 -0
- package/src/utils/authPopup.ts +95 -0
- package/src/utils/googleIdentity.ts +100 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { getENVVariable } from '@jsb188/app';
|
|
2
|
+
import type { OnErrorGQLFn } from '@jsb188/graphql/types.d';
|
|
3
|
+
import { AppContext, onAccountLogin, useOpenModalPopUp } from '@jsb188/react/states';
|
|
4
|
+
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
5
|
+
import { authenticateOAuthSignInSession } from '../modules/_helpers';
|
|
6
|
+
import { loadGoogleIdentityServices } from '../utils/googleIdentity';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Types
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// SSO sign-in option resolved by the getSsoLoginOptions mutation
|
|
13
|
+
export interface SsoLoginOptionData {
|
|
14
|
+
id: string;
|
|
15
|
+
provider: 'GOOGLE' | 'MICROSOFT';
|
|
16
|
+
organizationId: string;
|
|
17
|
+
organizationName: string;
|
|
18
|
+
microsoftTenantId?: string | null;
|
|
19
|
+
googleHostedDomain?: string | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Called when an "authenticateOnly" sign-in verified an email with no account
|
|
23
|
+
// and the server returned a temporary form respondent token instead
|
|
24
|
+
type OnRespondentTokenFn = (token: string, email: string | null) => void;
|
|
25
|
+
|
|
26
|
+
export type OnFinishSignInFn = (
|
|
27
|
+
token: string,
|
|
28
|
+
sessionBootstrapToken?: string | null,
|
|
29
|
+
sessionPersisted?: boolean,
|
|
30
|
+
) => boolean | Promise<boolean>;
|
|
31
|
+
|
|
32
|
+
interface ContinueWithGoogleParams {
|
|
33
|
+
doNotAlertErrors?: boolean;
|
|
34
|
+
onFinishSignIn: OnFinishSignInFn;
|
|
35
|
+
onRespondentToken?: OnRespondentTokenFn;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ContinueWithMicrosoftParams {
|
|
39
|
+
doNotAlertErrors?: boolean;
|
|
40
|
+
onFinishSignIn: OnFinishSignInFn;
|
|
41
|
+
onRespondentToken?: OnRespondentTokenFn;
|
|
42
|
+
onError?: OnErrorGQLFn;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface GoogleLoginHookParams extends ContinueWithGoogleParams {
|
|
46
|
+
scriptLoaded: boolean;
|
|
47
|
+
scope?: string;
|
|
48
|
+
onError?: OnErrorGQLFn;
|
|
49
|
+
onNonOAuthError?: (err: any) => void;
|
|
50
|
+
overrideScope?: boolean;
|
|
51
|
+
state?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
* Apply a BFF-authenticated provider result to the caller and shared app state.
|
|
56
|
+
*/
|
|
57
|
+
async function applyOAuthAuthenticationResult(
|
|
58
|
+
auth: any,
|
|
59
|
+
onFinishSignIn: OnFinishSignInFn,
|
|
60
|
+
onRespondentToken: OnRespondentTokenFn | undefined,
|
|
61
|
+
dispatchApp: any,
|
|
62
|
+
): Promise<boolean> {
|
|
63
|
+
if (auth?.token && auth.account?.id) {
|
|
64
|
+
const persisted = await onFinishSignIn(auth.token, null, true);
|
|
65
|
+
if (persisted) {
|
|
66
|
+
onAccountLogin({ ...auth, sessionBootstrapToken: null }, dispatchApp);
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
} else if (auth?.respondentToken) {
|
|
70
|
+
onRespondentToken?.(auth.respondentToken, auth.respondentEmail || null);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Google OAuth login
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
81
|
+
const {
|
|
82
|
+
scriptLoaded,
|
|
83
|
+
scope = '',
|
|
84
|
+
onError,
|
|
85
|
+
onNonOAuthError,
|
|
86
|
+
onFinishSignIn,
|
|
87
|
+
onRespondentToken,
|
|
88
|
+
overrideScope,
|
|
89
|
+
state,
|
|
90
|
+
doNotAlertErrors,
|
|
91
|
+
...props
|
|
92
|
+
} = p;
|
|
93
|
+
|
|
94
|
+
const { dispatchApp } = useContext(AppContext);
|
|
95
|
+
const openModalPopUp = useOpenModalPopUp();
|
|
96
|
+
const [waitingForProvider, setWaitingForProvider] = useState(false);
|
|
97
|
+
const [waitingForOAuth, setWaitingForOAuth] = useState(false);
|
|
98
|
+
|
|
99
|
+
const clientRef = useRef<any>(null);
|
|
100
|
+
const onErrorRef = useRef(onError);
|
|
101
|
+
|
|
102
|
+
onErrorRef.current = onError;
|
|
103
|
+
|
|
104
|
+
const onNonOAuthErrorRef = useRef(onNonOAuthError);
|
|
105
|
+
onNonOAuthErrorRef.current = onNonOAuthError;
|
|
106
|
+
|
|
107
|
+
const onRespondentTokenRef = useRef(onRespondentToken);
|
|
108
|
+
onRespondentTokenRef.current = onRespondentToken;
|
|
109
|
+
|
|
110
|
+
const onFinishSignInRef = useRef(onFinishSignIn);
|
|
111
|
+
onFinishSignInRef.current = onFinishSignIn;
|
|
112
|
+
|
|
113
|
+
const openModalPopUpRef = useRef(openModalPopUp);
|
|
114
|
+
openModalPopUpRef.current = openModalPopUp;
|
|
115
|
+
|
|
116
|
+
const doNotAlertErrorsRef = useRef(doNotAlertErrors);
|
|
117
|
+
doNotAlertErrorsRef.current = doNotAlertErrors;
|
|
118
|
+
|
|
119
|
+
const dispatchAppRef = useRef(dispatchApp);
|
|
120
|
+
dispatchAppRef.current = dispatchApp;
|
|
121
|
+
|
|
122
|
+
// Org SSO connection to sign in through; read by the token callback below
|
|
123
|
+
const ssoConnectionIdRef = useRef<string | null>(null);
|
|
124
|
+
// Public web form context; allows contact-gated account creation (CHILD_ORGS forms)
|
|
125
|
+
const formPublicIdRef = useRef<string | null>(null);
|
|
126
|
+
// When true, a verified email with no account returns a temporary respondent
|
|
127
|
+
// token instead of erroring (ANYONE web forms)
|
|
128
|
+
const authenticateOnlyRef = useRef<boolean>(false);
|
|
129
|
+
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
if (scriptLoaded) {
|
|
132
|
+
// If using this package, it is expected that you install your own Google OAuth script
|
|
133
|
+
const client = (globalThis as any).google?.accounts?.oauth2.initTokenClient({
|
|
134
|
+
client_id: getENVVariable('GOOGLE_OAUTH'),
|
|
135
|
+
scope: overrideScope ? scope : `openid profile email ${scope}`,
|
|
136
|
+
callback: async (response: any) => {
|
|
137
|
+
if (response.error) {
|
|
138
|
+
setWaitingForProvider(false);
|
|
139
|
+
setWaitingForOAuth(false);
|
|
140
|
+
return onErrorRef.current?.(response, null);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const { access_token: oauthToken } = response;
|
|
144
|
+
setWaitingForProvider(false);
|
|
145
|
+
setWaitingForOAuth(true);
|
|
146
|
+
const variables = {
|
|
147
|
+
provider: 'GOOGLE' as const,
|
|
148
|
+
oauthToken,
|
|
149
|
+
ssoConnectionId: ssoConnectionIdRef.current,
|
|
150
|
+
formPublicId: formPublicIdRef.current,
|
|
151
|
+
authenticateOnly: authenticateOnlyRef.current,
|
|
152
|
+
};
|
|
153
|
+
const { auth, error } = await authenticateOAuthSignInSession(variables);
|
|
154
|
+
if (error) {
|
|
155
|
+
setWaitingForOAuth(false);
|
|
156
|
+
if (!doNotAlertErrorsRef.current) {
|
|
157
|
+
openModalPopUpRef.current(null, error);
|
|
158
|
+
}
|
|
159
|
+
onErrorRef.current?.(error, variables);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const authenticated = await applyOAuthAuthenticationResult(
|
|
165
|
+
auth,
|
|
166
|
+
onFinishSignInRef.current,
|
|
167
|
+
onRespondentTokenRef.current,
|
|
168
|
+
dispatchAppRef.current,
|
|
169
|
+
);
|
|
170
|
+
if (!authenticated) {
|
|
171
|
+
setWaitingForOAuth(false);
|
|
172
|
+
}
|
|
173
|
+
} catch (error) {
|
|
174
|
+
setWaitingForOAuth(false);
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
error_callback: (nonOAuthError: any) => {
|
|
179
|
+
setWaitingForProvider(false);
|
|
180
|
+
setWaitingForOAuth(false);
|
|
181
|
+
onNonOAuthErrorRef.current?.(nonOAuthError);
|
|
182
|
+
},
|
|
183
|
+
state,
|
|
184
|
+
...props,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
clientRef.current = client;
|
|
188
|
+
}
|
|
189
|
+
}, [scriptLoaded, scope, state]);
|
|
190
|
+
|
|
191
|
+
const onInitiateGoogleSignIn = useCallback(
|
|
192
|
+
(overrideConfig?: object, ssoConnectionId?: string | null, formPublicId?: string | null, authenticateOnly?: boolean) => {
|
|
193
|
+
ssoConnectionIdRef.current = ssoConnectionId || null;
|
|
194
|
+
formPublicIdRef.current = formPublicId || null;
|
|
195
|
+
authenticateOnlyRef.current = !!authenticateOnly;
|
|
196
|
+
if (clientRef.current) {
|
|
197
|
+
setWaitingForProvider(true);
|
|
198
|
+
try {
|
|
199
|
+
clientRef.current.requestAccessToken(overrideConfig);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
setWaitingForProvider(false);
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
[],
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
onInitiateGoogleSignIn,
|
|
211
|
+
oauthButtonDisabled: waitingForProvider,
|
|
212
|
+
waitingForOAuth,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Web sign in with Google
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
export function useContinueWithGoogle(params: ContinueWithGoogleParams) {
|
|
221
|
+
const { doNotAlertErrors, onFinishSignIn, onRespondentToken } = params;
|
|
222
|
+
const [scriptLoaded, setScriptLoaded] = useState(false);
|
|
223
|
+
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
let active = true;
|
|
226
|
+
loadGoogleIdentityServices().then(
|
|
227
|
+
() => {
|
|
228
|
+
if (active) setScriptLoaded(true);
|
|
229
|
+
},
|
|
230
|
+
() => {
|
|
231
|
+
if (active) setScriptLoaded(false);
|
|
232
|
+
},
|
|
233
|
+
);
|
|
234
|
+
return () => {
|
|
235
|
+
active = false;
|
|
236
|
+
};
|
|
237
|
+
}, []);
|
|
238
|
+
|
|
239
|
+
const { onInitiateGoogleSignIn, oauthButtonDisabled, waitingForOAuth } = useGoogleLogin({
|
|
240
|
+
doNotAlertErrors,
|
|
241
|
+
onFinishSignIn,
|
|
242
|
+
onRespondentToken,
|
|
243
|
+
scriptLoaded,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
oauthButtonDisabled,
|
|
248
|
+
waitingForOAuth,
|
|
249
|
+
onInitiateGoogleSignIn,
|
|
250
|
+
scriptLoaded,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// MSAL client is cached at module level so repeated hook mounts reuse one instance
|
|
255
|
+
let msalClientPromise: Promise<any> | null = null;
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Get or lazily create the MSAL public client application
|
|
259
|
+
*/
|
|
260
|
+
|
|
261
|
+
function getMSALClient() {
|
|
262
|
+
if (!msalClientPromise) {
|
|
263
|
+
// Dynamic import keeps MSAL out of the main bundle until Microsoft sign in is used
|
|
264
|
+
msalClientPromise = import('@azure/msal-browser')
|
|
265
|
+
.then(async ({ PublicClientApplication }) => {
|
|
266
|
+
const pca = new PublicClientApplication({
|
|
267
|
+
auth: {
|
|
268
|
+
clientId: String(getENVVariable('MICROSOFT_OAUTH') || ''),
|
|
269
|
+
authority: 'https://login.microsoftonline.com/common',
|
|
270
|
+
redirectUri: getENVVariable('APP_URL') + 'auth/msal',
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
await pca.initialize();
|
|
275
|
+
return pca;
|
|
276
|
+
})
|
|
277
|
+
.catch((err) => {
|
|
278
|
+
// Do not cache a failed load/initialization; let the next click retry
|
|
279
|
+
msalClientPromise = null;
|
|
280
|
+
throw err;
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return msalClientPromise;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Web sign in with Microsoft
|
|
289
|
+
*/
|
|
290
|
+
|
|
291
|
+
export function useContinueWithMicrosoft(params: ContinueWithMicrosoftParams) {
|
|
292
|
+
const { doNotAlertErrors, onFinishSignIn, onRespondentToken, onError } = params;
|
|
293
|
+
const { dispatchApp } = useContext(AppContext);
|
|
294
|
+
const openModalPopUp = useOpenModalPopUp();
|
|
295
|
+
const [waitingForPopUp, setWaitingForPopUp] = useState(false);
|
|
296
|
+
const [waitingForOAuth, setWaitingForOAuth] = useState(false);
|
|
297
|
+
|
|
298
|
+
const onRespondentTokenRef = useRef(onRespondentToken);
|
|
299
|
+
onRespondentTokenRef.current = onRespondentToken;
|
|
300
|
+
|
|
301
|
+
const onFinishSignInRef = useRef(onFinishSignIn);
|
|
302
|
+
onFinishSignInRef.current = onFinishSignIn;
|
|
303
|
+
|
|
304
|
+
const onErrorRef = useRef(onError);
|
|
305
|
+
onErrorRef.current = onError;
|
|
306
|
+
|
|
307
|
+
const openModalPopUpRef = useRef(openModalPopUp);
|
|
308
|
+
openModalPopUpRef.current = openModalPopUp;
|
|
309
|
+
|
|
310
|
+
const doNotAlertErrorsRef = useRef(doNotAlertErrors);
|
|
311
|
+
doNotAlertErrorsRef.current = doNotAlertErrors;
|
|
312
|
+
|
|
313
|
+
const dispatchAppRef = useRef(dispatchApp);
|
|
314
|
+
dispatchAppRef.current = dispatchApp;
|
|
315
|
+
|
|
316
|
+
const onInitiateMicrosoftSignIn = useCallback(
|
|
317
|
+
async (ssoOption?: SsoLoginOptionData | any, loginHint?: string, formPublicId?: string | null, authenticateOnly?: boolean) => {
|
|
318
|
+
setWaitingForPopUp(true);
|
|
319
|
+
|
|
320
|
+
// A plain onClick passes the click event here; only a real SSO option
|
|
321
|
+
// (with a tenant id) changes the popup behavior
|
|
322
|
+
const ssoTenantId = ssoOption?.microsoftTenantId || null;
|
|
323
|
+
const ssoConnectionId = ssoTenantId ? ssoOption?.id : null;
|
|
324
|
+
|
|
325
|
+
try {
|
|
326
|
+
const pca = await getMSALClient();
|
|
327
|
+
const result = await pca.loginPopup({
|
|
328
|
+
scopes: ['openid', 'profile', 'email'],
|
|
329
|
+
prompt: 'select_account',
|
|
330
|
+
// Pin the popup to the org's Entra tenant instead of "common"
|
|
331
|
+
...(ssoTenantId ? { authority: `https://login.microsoftonline.com/${ssoTenantId}` } : null),
|
|
332
|
+
...(loginHint ? { loginHint } : null),
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
if (result?.idToken) {
|
|
336
|
+
setWaitingForOAuth(true);
|
|
337
|
+
const variables = {
|
|
338
|
+
provider: 'MICROSOFT' as const,
|
|
339
|
+
idToken: result.idToken,
|
|
340
|
+
ssoConnectionId,
|
|
341
|
+
formPublicId,
|
|
342
|
+
authenticateOnly,
|
|
343
|
+
};
|
|
344
|
+
const authentication = await authenticateOAuthSignInSession(variables);
|
|
345
|
+
if (authentication.error) {
|
|
346
|
+
setWaitingForOAuth(false);
|
|
347
|
+
if (!doNotAlertErrorsRef.current) {
|
|
348
|
+
openModalPopUpRef.current(null, authentication.error);
|
|
349
|
+
}
|
|
350
|
+
onErrorRef.current?.(authentication.error, variables);
|
|
351
|
+
} else {
|
|
352
|
+
const authenticated = await applyOAuthAuthenticationResult(
|
|
353
|
+
authentication.auth,
|
|
354
|
+
onFinishSignInRef.current,
|
|
355
|
+
onRespondentTokenRef.current,
|
|
356
|
+
dispatchAppRef.current,
|
|
357
|
+
);
|
|
358
|
+
if (!authenticated) {
|
|
359
|
+
setWaitingForOAuth(false);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
} catch (err: any) {
|
|
364
|
+
setWaitingForOAuth(false);
|
|
365
|
+
// User closing/cancelling the popup is not an error
|
|
366
|
+
if (err?.errorCode !== 'user_cancelled') {
|
|
367
|
+
console.warn(err);
|
|
368
|
+
}
|
|
369
|
+
} finally {
|
|
370
|
+
setWaitingForPopUp(false);
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
[],
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
return {
|
|
377
|
+
onInitiateMicrosoftSignIn,
|
|
378
|
+
oauthButtonDisabled: waitingForPopUp,
|
|
379
|
+
waitingForOAuth,
|
|
380
|
+
};
|
|
381
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export * from './hooks/use-auth-popup';
|
|
2
|
+
export * from './hooks/use-oauth';
|
|
3
|
+
export * from './hooks/use-google-one-tap';
|
|
4
|
+
export { suppressGoogleAutoSignIn } from './utils/googleIdentity';
|
|
5
|
+
export {
|
|
6
|
+
getAuthReturnQuery,
|
|
7
|
+
getAuthReturnContext,
|
|
8
|
+
getPublicFormReturnContext,
|
|
9
|
+
hydrateAuthenticationSession,
|
|
10
|
+
persistSignInSession,
|
|
11
|
+
} from './modules/_helpers';
|
|
12
|
+
export * from './modules/AssignRequiredAuth';
|
|
13
|
+
export * from './modules/AuthForms';
|
|
14
|
+
export * from './modules/AuthFormUI';
|
|
15
|
+
export * from './modules/AuthGate';
|
|
16
|
+
export { default as InlineAuthForm } from './modules/InlineAuthForm';
|
|
17
|
+
export * from './modules/ManagedSessions';
|
|
18
|
+
export * from './modules/OtherAuthButtons';
|
|
19
|
+
export * from './modules/schemas';
|
|
20
|
+
export * from './ui/ManagedSessionUI';
|
|
21
|
+
export * from './utils/authPopup';
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { useRequestChangeEmail } from '@jsb188/graphql/hooks/use-account-mtn';
|
|
3
|
+
import { useChangeAccountPassword, useRequestTokenizedEmail } from '@jsb188/graphql/hooks/use-auth-mtn';
|
|
4
|
+
import { AppContext } from '@jsb188/react/states';
|
|
5
|
+
import { FullPageLayout } from '@jsb188/react-web/modules/layout/MainLayout';
|
|
6
|
+
import SchemaForm from '@jsb188/form/libs/SchemaForm';
|
|
7
|
+
import { submissionResult } from '@jsb188/form/libs/validation';
|
|
8
|
+
import { COMMON_ICON_NAMES } from '@jsb188/react-web/svgs/Icon';
|
|
9
|
+
import { FullWidthButton } from '@jsb188/react-web/ui/Button';
|
|
10
|
+
import Markdown from '@jsb188/react-web/ui/Markdown';
|
|
11
|
+
import { ModalErrorMessage, ModalSimpleContent } from '@jsb188/react-web/ui/ModalUI';
|
|
12
|
+
import { useContext, useMemo, useState } from 'react';
|
|
13
|
+
import { AuthFormContainer } from './AuthFormUI';
|
|
14
|
+
import { makeAssignPasswordSchema, makeRequestTokenizedEmailSchema } from './schemas';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Types
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
interface FinishedResultObj {
|
|
21
|
+
hasPassword: boolean;
|
|
22
|
+
email: any;
|
|
23
|
+
emailVerified: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface RequiredAuthSwitchCaseProps {
|
|
27
|
+
identifier: string;
|
|
28
|
+
onFinish: () => void;
|
|
29
|
+
finishedResult: FinishedResultObj;
|
|
30
|
+
setFinishedResult: any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Account secured message; after all required auth info has been provided
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
function AccountSecuredMessage(p: RequiredAuthSwitchCaseProps) {
|
|
38
|
+
const { onFinish } = p;
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<ModalSimpleContent
|
|
42
|
+
// title='Finished!'
|
|
43
|
+
iconName={COMMON_ICON_NAMES.verified}
|
|
44
|
+
className="px_df pt_md"
|
|
45
|
+
>
|
|
46
|
+
<Markdown as="p">{i18n.t('auth.required_auth_fin_msg')}</Markdown>
|
|
47
|
+
|
|
48
|
+
<FullWidthButton
|
|
49
|
+
preset="bg_primary"
|
|
50
|
+
className="mt_md shadow"
|
|
51
|
+
onClick={onFinish}
|
|
52
|
+
>
|
|
53
|
+
{i18n.t('auth.continue_to_app')}
|
|
54
|
+
</FullWidthButton>
|
|
55
|
+
</ModalSimpleContent>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Set auth password form
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
function SetPasswordForm(p: RequiredAuthSwitchCaseProps) {
|
|
64
|
+
const { identifier, setFinishedResult } = p;
|
|
65
|
+
const assignPasswordSchema = useMemo(makeAssignPasswordSchema, []);
|
|
66
|
+
|
|
67
|
+
const { changeAccountPassword, saving, error, setError, resetErrors } = useChangeAccountPassword({
|
|
68
|
+
doNotFinishForm: true,
|
|
69
|
+
onCompleted: (passwordChanged: boolean) => {
|
|
70
|
+
if (passwordChanged) {
|
|
71
|
+
setFinishedResult((fr: FinishedResultObj) => ({
|
|
72
|
+
...fr,
|
|
73
|
+
hasPassword: true,
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/* Await the password mutation before completing this form step. */
|
|
80
|
+
const onSubmit = async (fv: { password: string; passwordRepeat: string }) => {
|
|
81
|
+
const result = await changeAccountPassword({
|
|
82
|
+
variables: {
|
|
83
|
+
currentPassword: '',
|
|
84
|
+
password: fv.password,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
return submissionResult(result);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<>
|
|
92
|
+
<p className="a_c mx_df mb_df -mt_5 pb_xs">{i18n.t('auth.add_password_sign_in_msg', { identifier })}</p>
|
|
93
|
+
|
|
94
|
+
<SchemaForm
|
|
95
|
+
className="pb_df fs"
|
|
96
|
+
busy={saving}
|
|
97
|
+
definition={assignPasswordSchema}
|
|
98
|
+
onError={setError}
|
|
99
|
+
onSubmit={onSubmit}
|
|
100
|
+
buttonText={i18n.t('auth.add_password')}
|
|
101
|
+
>
|
|
102
|
+
{!error ? null : (
|
|
103
|
+
<ModalErrorMessage
|
|
104
|
+
error={error}
|
|
105
|
+
resetErrors={resetErrors}
|
|
106
|
+
/>
|
|
107
|
+
)}
|
|
108
|
+
</SchemaForm>
|
|
109
|
+
</>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Set email form
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
function SetEmailForm(p: RequiredAuthSwitchCaseProps) {
|
|
118
|
+
const { finishedResult, identifier } = p;
|
|
119
|
+
const [waitingForVerification, setWaitingForVerification] = useState(false);
|
|
120
|
+
const assignPasswordSchema = useMemo(makeRequestTokenizedEmailSchema, []);
|
|
121
|
+
const existingEmailAddress = finishedResult.email?.address || '';
|
|
122
|
+
const currentData = useMemo(() => ({ email: existingEmailAddress }), [existingEmailAddress]);
|
|
123
|
+
const requiresExistingEmailVerification = !!existingEmailAddress && !finishedResult.emailVerified;
|
|
124
|
+
|
|
125
|
+
/*
|
|
126
|
+
* Show the check-email state after either email request succeeds.
|
|
127
|
+
*/
|
|
128
|
+
const onEmailRequested = (result: any) => {
|
|
129
|
+
if (result?.requestChangeEmail || result?.requestTokenizedEmail) {
|
|
130
|
+
setWaitingForVerification(true);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const changeEmail = useRequestChangeEmail();
|
|
135
|
+
const verifyEmail = useRequestTokenizedEmail();
|
|
136
|
+
const error = changeEmail.error || verifyEmail.error;
|
|
137
|
+
const saving = changeEmail.saving || verifyEmail.saving;
|
|
138
|
+
|
|
139
|
+
/*
|
|
140
|
+
* Clear errors from both possible email-request mutations.
|
|
141
|
+
*/
|
|
142
|
+
const resetErrors = () => {
|
|
143
|
+
changeEmail.resetErrors();
|
|
144
|
+
verifyEmail.resetErrors();
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/*
|
|
148
|
+
* Send a verification link for the current address, or a change-email link
|
|
149
|
+
* when the user supplies a different address.
|
|
150
|
+
*/
|
|
151
|
+
const onSubmit = async (fv: { email: string }) => {
|
|
152
|
+
const emailAddress = fv.email.trim();
|
|
153
|
+
if (emailAddress.toLowerCase() === existingEmailAddress.trim().toLowerCase()) {
|
|
154
|
+
const result = await verifyEmail.requestTokenizedEmail({
|
|
155
|
+
variables: {
|
|
156
|
+
email: emailAddress,
|
|
157
|
+
requestType: 'EMAIL_VERIFICATION',
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
onEmailRequested(result);
|
|
161
|
+
return submissionResult(result);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const result = await changeEmail.requestChangeEmail({
|
|
165
|
+
variables: {
|
|
166
|
+
emailAddress,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
onEmailRequested(result);
|
|
170
|
+
return submissionResult(result);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
if (waitingForVerification) {
|
|
174
|
+
return (
|
|
175
|
+
<ModalSimpleContent
|
|
176
|
+
className="px_df pt_md pb_25"
|
|
177
|
+
iconName={COMMON_ICON_NAMES.email_address}
|
|
178
|
+
title={i18n.t('auth.check_email')}
|
|
179
|
+
message={i18n.t('auth.check_email_to_login_msg')}
|
|
180
|
+
/>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<>
|
|
186
|
+
<p className="a_c mx_df mb_df -mt_5 pb_xs">
|
|
187
|
+
{i18n.t(requiresExistingEmailVerification ? 'auth.verify_email_to_login_msg' : 'auth.add_email_to_login_msg', {
|
|
188
|
+
identifier,
|
|
189
|
+
})}
|
|
190
|
+
</p>
|
|
191
|
+
|
|
192
|
+
<SchemaForm
|
|
193
|
+
className="pb_df fs"
|
|
194
|
+
initialValues={currentData}
|
|
195
|
+
busy={saving}
|
|
196
|
+
definition={assignPasswordSchema}
|
|
197
|
+
onError={changeEmail.setError}
|
|
198
|
+
onSubmit={onSubmit}
|
|
199
|
+
buttonText={i18n.t(requiresExistingEmailVerification ? 'auth.resend_verification' : 'auth.add_email')}
|
|
200
|
+
>
|
|
201
|
+
{!error ? null : (
|
|
202
|
+
<ModalErrorMessage
|
|
203
|
+
error={error}
|
|
204
|
+
resetErrors={resetErrors}
|
|
205
|
+
/>
|
|
206
|
+
)}
|
|
207
|
+
</SchemaForm>
|
|
208
|
+
</>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Switch case for required auth
|
|
214
|
+
*/
|
|
215
|
+
|
|
216
|
+
function RequiredAuthSwitchCase(p: RequiredAuthSwitchCaseProps) {
|
|
217
|
+
const { finishedResult } = p;
|
|
218
|
+
|
|
219
|
+
if (!finishedResult.hasPassword) {
|
|
220
|
+
return <SetPasswordForm {...p} />;
|
|
221
|
+
}
|
|
222
|
+
if (!finishedResult.emailVerified) {
|
|
223
|
+
// Keep the email check last because it has a verification step.
|
|
224
|
+
return <SetEmailForm {...p} />;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return <AccountSecuredMessage {...p} />;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Force users to provide required authentication info
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
export function AssignRequiredAuth(p: { hasPassword: boolean; hasVerifiedEmail: boolean }) {
|
|
235
|
+
const {
|
|
236
|
+
dispatchApp,
|
|
237
|
+
appState: { hasPassword, account },
|
|
238
|
+
} = useContext(AppContext);
|
|
239
|
+
const identifier = account?.email?.address || account?.phone?.number || account?.id;
|
|
240
|
+
|
|
241
|
+
const [finishedResult, setFinishedResult] = useState({
|
|
242
|
+
hasPassword,
|
|
243
|
+
email: account?.email?.address ? account?.email : null,
|
|
244
|
+
emailVerified: p.hasVerifiedEmail,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const onFinish = () => {
|
|
248
|
+
if (!hasPassword) {
|
|
249
|
+
dispatchApp({
|
|
250
|
+
__type: 'APP_EDIT_AUTH',
|
|
251
|
+
data: {
|
|
252
|
+
id: account?.id,
|
|
253
|
+
...finishedResult,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
return (
|
|
260
|
+
<FullPageLayout className="pattern_dot active_bf bg_fade">
|
|
261
|
+
<AuthFormContainer
|
|
262
|
+
title={i18n.t('auth.one_last_step_')}
|
|
263
|
+
className="bd_5 bd_main"
|
|
264
|
+
>
|
|
265
|
+
<RequiredAuthSwitchCase
|
|
266
|
+
identifier={identifier!}
|
|
267
|
+
finishedResult={finishedResult}
|
|
268
|
+
setFinishedResult={setFinishedResult}
|
|
269
|
+
onFinish={onFinish}
|
|
270
|
+
/>
|
|
271
|
+
</AuthFormContainer>
|
|
272
|
+
</FullPageLayout>
|
|
273
|
+
);
|
|
274
|
+
}
|