@jsb188/auth-web 1.1.12 → 1.1.13
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/LICENSE +21 -0
- package/package.json +16 -8
- package/src/hooks/use-auth-popup.ts +2 -27
- package/src/hooks/use-oauth.tsx +64 -118
- package/src/main.ts +0 -3
- package/src/modules/AuthForms-SSO.tsx +6 -14
- package/src/modules/AuthForms.tsx +52 -107
- package/src/modules/InlineAuthForm.tsx +57 -122
- package/src/modules/ManagedSessions.tsx +6 -3
- package/src/modules/OtherAuthButtons.tsx +23 -30
- package/src/modules/_helpers.ts +1 -23
- package/src/schemas/address.ts +10 -4
- package/src/utils/authPopup.ts +0 -60
- package/src/utils/googleIdentity.ts +6 -61
- package/src/hooks/use-google-one-tap.ts +0 -138
- package/src/modules/ConfirmPublicFormEmailCode.tsx +0 -144
|
@@ -1,41 +1,22 @@
|
|
|
1
1
|
const FILE_CONSTANTS = {
|
|
2
2
|
scriptUrl: 'https://accounts.google.com/gsi/client',
|
|
3
3
|
scriptTimeoutMs: 10000,
|
|
4
|
-
signInStateKey: 'marketday:google-sign-in',
|
|
5
4
|
};
|
|
6
5
|
|
|
7
|
-
export interface GoogleOneTapClient {
|
|
8
|
-
initialize: (options: {
|
|
9
|
-
client_id: string;
|
|
10
|
-
auto_select: boolean;
|
|
11
|
-
nonce: string;
|
|
12
|
-
callback: (response: { credential: string }) => void;
|
|
13
|
-
}) => void;
|
|
14
|
-
prompt: (
|
|
15
|
-
callback: (notification: {
|
|
16
|
-
isSkippedMoment: () => boolean;
|
|
17
|
-
isDismissedMoment: () => boolean;
|
|
18
|
-
getDismissedReason: () => string;
|
|
19
|
-
}) => void,
|
|
20
|
-
) => void;
|
|
21
|
-
cancel: () => void;
|
|
22
|
-
disableAutoSelect: () => void;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
6
|
let googleScriptPromise: Promise<void> | null = null;
|
|
26
7
|
|
|
27
8
|
/*
|
|
28
|
-
* Return the
|
|
9
|
+
* Return the manual OAuth client after the shared Google SDK loads.
|
|
29
10
|
*/
|
|
30
|
-
|
|
31
|
-
return (globalThis as typeof globalThis & { google?: { accounts?: {
|
|
11
|
+
function getGoogleOAuthClient(): unknown {
|
|
12
|
+
return (globalThis as typeof globalThis & { google?: { accounts?: { oauth2?: unknown } } }).google?.accounts?.oauth2;
|
|
32
13
|
}
|
|
33
14
|
|
|
34
15
|
/*
|
|
35
|
-
* Load one shared Google SDK for manual OAuth
|
|
16
|
+
* Load one shared Google SDK for manual OAuth, with a bounded failure path.
|
|
36
17
|
*/
|
|
37
18
|
export function loadGoogleIdentityServices(): Promise<void> {
|
|
38
|
-
if (
|
|
19
|
+
if (getGoogleOAuthClient()) return Promise.resolve();
|
|
39
20
|
if (googleScriptPromise) return googleScriptPromise;
|
|
40
21
|
googleScriptPromise = new Promise<void>((resolve, reject) => {
|
|
41
22
|
const script = document.createElement('script');
|
|
@@ -56,45 +37,9 @@ export function loadGoogleIdentityServices(): Promise<void> {
|
|
|
56
37
|
script.src = FILE_CONSTANTS.scriptUrl;
|
|
57
38
|
script.async = true;
|
|
58
39
|
script.defer = true;
|
|
59
|
-
script.onload = () => finish(!!
|
|
40
|
+
script.onload = () => finish(!!getGoogleOAuthClient());
|
|
60
41
|
script.onerror = () => finish(false);
|
|
61
42
|
document.body.appendChild(script);
|
|
62
43
|
});
|
|
63
44
|
return googleScriptPromise;
|
|
64
45
|
}
|
|
65
|
-
|
|
66
|
-
/*
|
|
67
|
-
* Read logout suppression without requiring prior Google sign-in or restricting the available Google accounts.
|
|
68
|
-
*/
|
|
69
|
-
export function getGoogleSignOutMarker(): string | null {
|
|
70
|
-
try {
|
|
71
|
-
const state = globalThis.localStorage?.getItem(FILE_CONSTANTS.signInStateKey);
|
|
72
|
-
return state === 'signed-out' || state?.startsWith('signed-out:') ? state : null;
|
|
73
|
-
} catch {
|
|
74
|
-
// One Tap can still be offered when storage is blocked, but require an explicit click.
|
|
75
|
-
return 'storage-unavailable';
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/*
|
|
80
|
-
* Allow automatic selection again after a successful Google authentication.
|
|
81
|
-
*/
|
|
82
|
-
export function clearGoogleAutoSignInSuppression() {
|
|
83
|
-
try {
|
|
84
|
-
globalThis.localStorage?.removeItem(FILE_CONSTANTS.signInStateKey);
|
|
85
|
-
} catch {
|
|
86
|
-
// Storage restrictions do not prevent an explicit One Tap sign-in.
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/*
|
|
91
|
-
* Keep an explicit logout from immediately signing the user back in on a public form.
|
|
92
|
-
*/
|
|
93
|
-
export function suppressGoogleAutoSignIn() {
|
|
94
|
-
try {
|
|
95
|
-
globalThis.localStorage?.setItem(FILE_CONSTANTS.signInStateKey, `signed-out:${crypto.randomUUID()}`);
|
|
96
|
-
} catch {
|
|
97
|
-
// Google also maintains its own sign-out suppression when the SDK is available.
|
|
98
|
-
}
|
|
99
|
-
getGoogleOneTapClient()?.disableAutoSelect();
|
|
100
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { getENVVariable } from '@jsb188/app';
|
|
2
|
-
import { AppContext, onAccountLogin } from '@jsb188/react/states';
|
|
3
|
-
import { useContext, useEffect, useRef, useState } from 'react';
|
|
4
|
-
import { authenticateOAuthSignInSession } from '../modules/_helpers';
|
|
5
|
-
import {
|
|
6
|
-
getGoogleSignOutMarker,
|
|
7
|
-
getGoogleOneTapClient,
|
|
8
|
-
type GoogleOneTapClient,
|
|
9
|
-
loadGoogleIdentityServices,
|
|
10
|
-
} from '../utils/googleIdentity';
|
|
11
|
-
import type { OnFinishSignInFn } from './use-oauth';
|
|
12
|
-
|
|
13
|
-
const FILE_CONSTANTS = {
|
|
14
|
-
promptTimeoutMs: 60000,
|
|
15
|
-
authenticationTimeoutMs: 30000,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/*
|
|
19
|
-
* Try One Tap once on an eligible public form and report progress through credential adoption.
|
|
20
|
-
*/
|
|
21
|
-
export function useGoogleOneTap(p: {
|
|
22
|
-
enabled: boolean;
|
|
23
|
-
formPublicId: string;
|
|
24
|
-
authenticateOnly: boolean;
|
|
25
|
-
onFinishSignIn: OnFinishSignInFn;
|
|
26
|
-
onRespondentToken: (token: string, email: string | null) => void;
|
|
27
|
-
}) {
|
|
28
|
-
const { dispatchApp } = useContext(AppContext);
|
|
29
|
-
const callbacks = useRef({ onFinishSignIn: p.onFinishSignIn, onRespondentToken: p.onRespondentToken, dispatchApp });
|
|
30
|
-
callbacks.current = { onFinishSignIn: p.onFinishSignIn, onRespondentToken: p.onRespondentToken, dispatchApp };
|
|
31
|
-
const completedForm = useRef<string | null>(null);
|
|
32
|
-
const [loading, setLoading] = useState(false);
|
|
33
|
-
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
const signOutMarker = getGoogleSignOutMarker();
|
|
36
|
-
if (!p.enabled || completedForm.current === p.formPublicId) {
|
|
37
|
-
setLoading(false);
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
const clientId = String(getENVVariable('GOOGLE_OAUTH') || '');
|
|
41
|
-
if (!clientId) return;
|
|
42
|
-
|
|
43
|
-
let active = true;
|
|
44
|
-
let authenticating = false;
|
|
45
|
-
let client: GoogleOneTapClient | undefined;
|
|
46
|
-
const controller = new AbortController();
|
|
47
|
-
let timer = globalThis.setTimeout(() => finish(), FILE_CONSTANTS.promptTimeoutMs);
|
|
48
|
-
setLoading(true);
|
|
49
|
-
|
|
50
|
-
/* End this attempt before cancellation can emit another SDK notification. */
|
|
51
|
-
const finish = () => {
|
|
52
|
-
if (!active) return;
|
|
53
|
-
active = false;
|
|
54
|
-
completedForm.current = p.formPublicId;
|
|
55
|
-
globalThis.clearTimeout(timer);
|
|
56
|
-
controller.abort();
|
|
57
|
-
client?.cancel();
|
|
58
|
-
setLoading(false);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/* Exchange a Google credential through the same-origin endpoint and adopt the verified identity. */
|
|
62
|
-
const onCredential = async (response: { credential: string }) => {
|
|
63
|
-
if (!active || authenticating || !response.credential) return;
|
|
64
|
-
if (getGoogleSignOutMarker() !== signOutMarker) return finish();
|
|
65
|
-
authenticating = true;
|
|
66
|
-
globalThis.clearTimeout(timer);
|
|
67
|
-
timer = globalThis.setTimeout(finish, FILE_CONSTANTS.authenticationTimeoutMs);
|
|
68
|
-
try {
|
|
69
|
-
const { auth, error } = await authenticateOAuthSignInSession(
|
|
70
|
-
{
|
|
71
|
-
provider: 'GOOGLE',
|
|
72
|
-
idToken: response.credential,
|
|
73
|
-
formPublicId: p.formPublicId,
|
|
74
|
-
authenticateOnly: p.authenticateOnly,
|
|
75
|
-
},
|
|
76
|
-
controller.signal,
|
|
77
|
-
);
|
|
78
|
-
if (!active || error) return;
|
|
79
|
-
if (auth?.account?.id && auth.token) {
|
|
80
|
-
const persisted = await callbacks.current.onFinishSignIn(auth.token, null, true);
|
|
81
|
-
if (active && persisted) onAccountLogin({ ...auth, sessionBootstrapToken: null }, callbacks.current.dispatchApp);
|
|
82
|
-
} else if (auth?.respondentToken) {
|
|
83
|
-
callbacks.current.onRespondentToken(auth.respondentToken, auth.respondentEmail || null);
|
|
84
|
-
}
|
|
85
|
-
} catch {
|
|
86
|
-
// Keep manual sign-in available if identity adoption fails.
|
|
87
|
-
} finally {
|
|
88
|
-
finish();
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/* Load Google and request a browser-bound nonce before asking Google to show One Tap. */
|
|
93
|
-
const start = async () => {
|
|
94
|
-
try {
|
|
95
|
-
await loadGoogleIdentityServices();
|
|
96
|
-
if (!active) return;
|
|
97
|
-
const response = await fetch('/auth/session', {
|
|
98
|
-
method: 'POST',
|
|
99
|
-
credentials: 'same-origin',
|
|
100
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
101
|
-
body: new URLSearchParams({ googleOneTapChallenge: '1', formPublicId: p.formPublicId }),
|
|
102
|
-
signal: controller.signal,
|
|
103
|
-
});
|
|
104
|
-
const challenge = await response.json();
|
|
105
|
-
if (!active) return;
|
|
106
|
-
if (getGoogleSignOutMarker() !== signOutMarker) return finish();
|
|
107
|
-
client = getGoogleOneTapClient();
|
|
108
|
-
if (!response.ok || !challenge.ok || typeof challenge.nonce !== 'string' || !client) return finish();
|
|
109
|
-
client.initialize({
|
|
110
|
-
client_id: clientId,
|
|
111
|
-
auto_select: !signOutMarker,
|
|
112
|
-
nonce: challenge.nonce,
|
|
113
|
-
callback: onCredential,
|
|
114
|
-
});
|
|
115
|
-
client.prompt((notification) => {
|
|
116
|
-
if (authenticating) return;
|
|
117
|
-
if (
|
|
118
|
-
notification.isSkippedMoment() ||
|
|
119
|
-
(notification.isDismissedMoment() && notification.getDismissedReason() !== 'credential_returned')
|
|
120
|
-
) {
|
|
121
|
-
finish();
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
} catch {
|
|
125
|
-
finish();
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
void start();
|
|
129
|
-
return () => {
|
|
130
|
-
active = false;
|
|
131
|
-
globalThis.clearTimeout(timer);
|
|
132
|
-
controller.abort();
|
|
133
|
-
client?.cancel();
|
|
134
|
-
};
|
|
135
|
-
}, [p.enabled, p.formPublicId, p.authenticateOnly]);
|
|
136
|
-
|
|
137
|
-
return { loading: p.enabled && loading };
|
|
138
|
-
}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
-
import { SMSCodeInput } from '@jsb188/form/modules/Form';
|
|
3
|
-
import { useConfirmPublicFormEmailCode, useSendPublicFormEmailCode } from '@jsb188/graphql/hooks/use-publicFormEmail-mtn';
|
|
4
|
-
import { ModalErrorMessage } from '@jsb188/react-web/ui/ModalUI';
|
|
5
|
-
import { useEffect, useState } from 'react';
|
|
6
|
-
|
|
7
|
-
/*
|
|
8
|
-
* Confirm an email code already sent by the public-form sign-in popup.
|
|
9
|
-
*/
|
|
10
|
-
export const ConfirmPublicFormEmailCode = (p: {
|
|
11
|
-
email: string;
|
|
12
|
-
formPublicId: string;
|
|
13
|
-
onRespondentToken: (token: string, email: string | null) => boolean;
|
|
14
|
-
onChangeEmail: () => void;
|
|
15
|
-
}) => {
|
|
16
|
-
const [resendAt, setResendAt] = useState(() => Date.now() + 60_000);
|
|
17
|
-
const [resendSeconds, setResendSeconds] = useState(60);
|
|
18
|
-
const [codeInputVersion, setCodeInputVersion] = useState(0);
|
|
19
|
-
const {
|
|
20
|
-
confirmPublicFormEmailCode,
|
|
21
|
-
saving: confirming,
|
|
22
|
-
error: confirmError,
|
|
23
|
-
setError: setConfirmError,
|
|
24
|
-
resetErrors: resetConfirmErrors,
|
|
25
|
-
} = useConfirmPublicFormEmailCode({
|
|
26
|
-
/* Hand only the temporary respondent identity back to the current form. */
|
|
27
|
-
onCompleted: (data) => {
|
|
28
|
-
const auth = data?.confirmPublicFormEmailCode;
|
|
29
|
-
if (auth?.respondentToken && auth.respondentEmail) {
|
|
30
|
-
if (!p.onRespondentToken(auth.respondentToken, auth.respondentEmail)) {
|
|
31
|
-
setConfirmError({ message: i18n.t('error.network_error') });
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
setConfirmError({ message: i18n.t('error.20219') });
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
const {
|
|
39
|
-
sendPublicFormEmailCode,
|
|
40
|
-
saving: sending,
|
|
41
|
-
error: sendError,
|
|
42
|
-
setError: setSendError,
|
|
43
|
-
resetErrors: resetSendErrors,
|
|
44
|
-
} = useSendPublicFormEmailCode({
|
|
45
|
-
/* Clear the old code and restart the cooldown after replacement delivery is accepted. */
|
|
46
|
-
onCompleted: (data) => {
|
|
47
|
-
if (data?.sendPublicFormEmailCode) {
|
|
48
|
-
setCodeInputVersion((version) => version + 1);
|
|
49
|
-
setResendAt(Date.now() + 60_000);
|
|
50
|
-
setResendSeconds(60);
|
|
51
|
-
} else {
|
|
52
|
-
setSendError({ message: i18n.t('error.30000') });
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
const saving = sending || confirming;
|
|
57
|
-
const error = confirmError || sendError;
|
|
58
|
-
|
|
59
|
-
useEffect(() => {
|
|
60
|
-
if (!resendAt) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/* Derive the cooldown from the clock so background tabs do not prolong it. */
|
|
65
|
-
const updateCountdown = () => {
|
|
66
|
-
const seconds = Math.max(0, Math.ceil((resendAt - Date.now()) / 1000));
|
|
67
|
-
setResendSeconds(seconds);
|
|
68
|
-
if (!seconds) {
|
|
69
|
-
setResendAt(0);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
const timer = globalThis.setInterval(updateCountdown, 500);
|
|
73
|
-
return () => globalThis.clearInterval(timer);
|
|
74
|
-
}, [resendAt]);
|
|
75
|
-
|
|
76
|
-
/* Clear errors before another request or returning to account discovery. */
|
|
77
|
-
const resetErrors = () => {
|
|
78
|
-
resetSendErrors();
|
|
79
|
-
resetConfirmErrors();
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/* Reuse the SMS input's numeric code, including leading zeroes, for email verification. */
|
|
83
|
-
const onConfirmCode = (code: string) => {
|
|
84
|
-
if (saving) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
resetErrors();
|
|
88
|
-
void confirmPublicFormEmailCode({ variables: { formPublicId: p.formPublicId, email: p.email, code } });
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
/* Request a replacement code only after the current cooldown has elapsed. */
|
|
92
|
-
const onResend = () => {
|
|
93
|
-
if (resendSeconds || saving) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
resetErrors();
|
|
97
|
-
void sendPublicFormEmailCode({ variables: { formPublicId: p.formPublicId, email: p.email } });
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/* Discover the replacement email's account before choosing password or code authentication. */
|
|
101
|
-
const onChangeEmail = () => {
|
|
102
|
-
resetErrors();
|
|
103
|
-
p.onChangeEmail();
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
return (
|
|
107
|
-
<>
|
|
108
|
-
{error ? (
|
|
109
|
-
<ModalErrorMessage
|
|
110
|
-
error={error}
|
|
111
|
-
resetErrors={resetErrors}
|
|
112
|
-
/>
|
|
113
|
-
) : null}
|
|
114
|
-
<SMSCodeInput
|
|
115
|
-
key={codeInputVersion}
|
|
116
|
-
codeLength={6}
|
|
117
|
-
saving={saving}
|
|
118
|
-
error={error}
|
|
119
|
-
onSubmit={onConfirmCode}
|
|
120
|
-
/>
|
|
121
|
-
<p className='a_c p_n mt_md mb_df rel'>{i18n.t('auth.public_form_email_code_sent', { emailAddress: p.email })}</p>
|
|
122
|
-
<div className='v_center gap_df f_wrap'>
|
|
123
|
-
<button
|
|
124
|
-
type='button'
|
|
125
|
-
disabled={saving || !!resendSeconds}
|
|
126
|
-
onClick={onResend}
|
|
127
|
-
className='text ft_sm cl_md link'
|
|
128
|
-
>
|
|
129
|
-
{resendSeconds
|
|
130
|
-
? i18n.t('auth.resend_verification_code_in', { seconds: resendSeconds })
|
|
131
|
-
: i18n.t('auth.resend_verification_code')}
|
|
132
|
-
</button>
|
|
133
|
-
<button
|
|
134
|
-
type='button'
|
|
135
|
-
disabled={saving}
|
|
136
|
-
onClick={onChangeEmail}
|
|
137
|
-
className='text ft_sm cl_md link'
|
|
138
|
-
>
|
|
139
|
-
{i18n.t('auth.use_different_email')}
|
|
140
|
-
</button>
|
|
141
|
-
</div>
|
|
142
|
-
</>
|
|
143
|
-
);
|
|
144
|
-
};
|