@jsb188/auth-web 1.1.11 → 1.1.12
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 +9 -7
- 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 +25 -51
- package/src/main.ts +11 -0
- package/src/modules/AssignRequiredAuth.tsx +93 -59
- package/src/modules/AuthFormUI.tsx +2 -2
- package/src/modules/AuthForms-SSO.tsx +43 -58
- package/src/modules/AuthForms.tsx +420 -287
- package/src/modules/AuthGate.tsx +8 -23
- package/src/modules/ConfirmPublicFormEmailCode.tsx +144 -0
- package/src/modules/InlineAuthForm.tsx +373 -75
- package/src/modules/ManagedSessions.tsx +22 -42
- package/src/modules/OtherAuthButtons.tsx +9 -7
- package/src/modules/_helpers.ts +94 -37
- package/src/modules/schemas.ts +114 -244
- package/src/schemas/account.ts +86 -149
- package/src/schemas/address.ts +108 -157
- package/src/utils/authPopup.ts +95 -0
- package/src/utils/googleIdentity.ts +100 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const FILE_CONSTANTS = {
|
|
2
|
+
scriptUrl: 'https://accounts.google.com/gsi/client',
|
|
3
|
+
scriptTimeoutMs: 10000,
|
|
4
|
+
signInStateKey: 'marketday:google-sign-in',
|
|
5
|
+
};
|
|
6
|
+
|
|
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
|
+
let googleScriptPromise: Promise<void> | null = null;
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* Return the official One Tap client after the shared Google SDK loads.
|
|
29
|
+
*/
|
|
30
|
+
export function getGoogleOneTapClient(): GoogleOneTapClient | undefined {
|
|
31
|
+
return (globalThis as typeof globalThis & { google?: { accounts?: { id?: GoogleOneTapClient } } }).google?.accounts?.id;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* Load one shared Google SDK for manual OAuth and route-scoped One Tap, with a bounded failure path.
|
|
36
|
+
*/
|
|
37
|
+
export function loadGoogleIdentityServices(): Promise<void> {
|
|
38
|
+
if (getGoogleOneTapClient()) return Promise.resolve();
|
|
39
|
+
if (googleScriptPromise) return googleScriptPromise;
|
|
40
|
+
googleScriptPromise = new Promise<void>((resolve, reject) => {
|
|
41
|
+
const script = document.createElement('script');
|
|
42
|
+
const timer = globalThis.setTimeout(() => finish(false), FILE_CONSTANTS.scriptTimeoutMs);
|
|
43
|
+
/* Clear script listeners and allow a later attempt after a failed SDK load. */
|
|
44
|
+
function finish(success: boolean) {
|
|
45
|
+
globalThis.clearTimeout(timer);
|
|
46
|
+
script.onload = null;
|
|
47
|
+
script.onerror = null;
|
|
48
|
+
if (success) {
|
|
49
|
+
resolve();
|
|
50
|
+
} else {
|
|
51
|
+
script.remove();
|
|
52
|
+
googleScriptPromise = null;
|
|
53
|
+
reject(new Error('Google Identity Services unavailable'));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
script.src = FILE_CONSTANTS.scriptUrl;
|
|
57
|
+
script.async = true;
|
|
58
|
+
script.defer = true;
|
|
59
|
+
script.onload = () => finish(!!getGoogleOneTapClient());
|
|
60
|
+
script.onerror = () => finish(false);
|
|
61
|
+
document.body.appendChild(script);
|
|
62
|
+
});
|
|
63
|
+
return googleScriptPromise;
|
|
64
|
+
}
|
|
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
|
+
}
|