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