@jsb188/auth-web 1.1.10 → 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 +27 -25
- 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 +314 -310
- package/src/main.ts +11 -0
- package/src/modules/AssignRequiredAuth.tsx +217 -171
- package/src/modules/AuthFormUI.tsx +28 -28
- package/src/modules/AuthForms-SSO.tsx +174 -152
- package/src/modules/AuthForms.tsx +814 -612
- package/src/modules/AuthGate.tsx +93 -85
- package/src/modules/ConfirmPublicFormEmailCode.tsx +144 -0
- package/src/modules/InlineAuthForm.tsx +441 -130
- package/src/modules/ManagedSessions.tsx +86 -80
- package/src/modules/OtherAuthButtons.tsx +72 -68
- package/src/modules/_helpers.ts +161 -86
- package/src/modules/schemas.ts +159 -260
- package/src/schemas/account.ts +106 -154
- package/src/schemas/address.ts +125 -157
- package/src/ui/ManagedSessionUI.tsx +70 -64
- package/src/utils/authPopup.ts +95 -0
- package/src/utils/googleIdentity.ts +100 -0
package/package.json
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "@jsb188/auth-web",
|
|
3
|
+
"version": "1.1.12",
|
|
4
|
+
"description": "Authentication module for web.",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "jsb188",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/main.ts",
|
|
10
|
+
"./schemas": "./src/schemas/index.ts",
|
|
11
|
+
"./schemas/address": "./src/schemas/address.ts"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"@azure/msal-browser": "^4.30.0",
|
|
15
|
+
"@jsb188/app": "1.1.12",
|
|
16
|
+
"@jsb188/graphql": "1.1.12",
|
|
17
|
+
"@jsb188/react": "1.1.12",
|
|
18
|
+
"@jsb188/react-web": "1.1.12",
|
|
19
|
+
"jotai": "^2.20.2",
|
|
20
|
+
"react": "^19.3.0",
|
|
21
|
+
"react-router": "^8.4.0",
|
|
22
|
+
"@jsb188/form": "1.1.12",
|
|
23
|
+
"zod": "^3.25.76"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
27
|
+
"build": "tsc"
|
|
28
|
+
}
|
|
27
29
|
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { hydrateAuthenticationSession } from '../modules/_helpers.ts';
|
|
3
|
+
import {
|
|
4
|
+
type AuthPopupCompletionMessage,
|
|
5
|
+
isAuthPopupCompletionMessage,
|
|
6
|
+
isAuthPopupRespondentCompletionMessage,
|
|
7
|
+
} from '../utils/authPopup.ts';
|
|
8
|
+
|
|
9
|
+
interface UseAuthenticationPopupParams {
|
|
10
|
+
href?: string | null;
|
|
11
|
+
formPublicId?: string | null;
|
|
12
|
+
onRespondentAuthenticated?: (token: string, email: string) => void | Promise<void>;
|
|
13
|
+
onAuthenticated: (auth: any, completionMessage: AuthPopupCompletionMessage) => boolean | Promise<boolean>;
|
|
14
|
+
onError?: (error: any) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* Open one same-origin authentication window and adopt its cookie-backed
|
|
19
|
+
* account session after the popup reports completion.
|
|
20
|
+
*/
|
|
21
|
+
export function useAuthenticationPopup(p: UseAuthenticationPopupParams) {
|
|
22
|
+
const [popupOpen, setPopupOpen] = useState(false);
|
|
23
|
+
const popupRef = useRef<Window | null>(null);
|
|
24
|
+
const popupIdRef = useRef<string | null>(null);
|
|
25
|
+
const popupFormPublicIdRef = useRef<string | null>(null);
|
|
26
|
+
const closeTimerRef = useRef<ReturnType<typeof globalThis.setInterval> | null>(null);
|
|
27
|
+
const completionStartedRef = useRef(false);
|
|
28
|
+
const onAuthenticatedRef = useRef(p.onAuthenticated);
|
|
29
|
+
const onErrorRef = useRef(p.onError);
|
|
30
|
+
const formPublicIdRef = useRef(p.formPublicId);
|
|
31
|
+
const onRespondentAuthenticatedRef = useRef(p.onRespondentAuthenticated);
|
|
32
|
+
|
|
33
|
+
onAuthenticatedRef.current = p.onAuthenticated;
|
|
34
|
+
onErrorRef.current = p.onError;
|
|
35
|
+
formPublicIdRef.current = p.formPublicId;
|
|
36
|
+
onRespondentAuthenticatedRef.current = p.onRespondentAuthenticated;
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* Clear the tracked popup and its close monitor after any terminal outcome.
|
|
40
|
+
*/
|
|
41
|
+
const resetPopup = useCallback(() => {
|
|
42
|
+
if (closeTimerRef.current !== null) {
|
|
43
|
+
globalThis.clearInterval(closeTimerRef.current);
|
|
44
|
+
closeTimerRef.current = null;
|
|
45
|
+
}
|
|
46
|
+
popupRef.current = null;
|
|
47
|
+
popupIdRef.current = null;
|
|
48
|
+
popupFormPublicIdRef.current = null;
|
|
49
|
+
completionStartedRef.current = false;
|
|
50
|
+
setPopupOpen(false);
|
|
51
|
+
}, []);
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
/*
|
|
55
|
+
* Validate a popup completion signal before rehydrating its shared session.
|
|
56
|
+
*/
|
|
57
|
+
const onMessage = (event: MessageEvent) => {
|
|
58
|
+
const popup = popupRef.current;
|
|
59
|
+
const popupId = popupIdRef.current;
|
|
60
|
+
if (
|
|
61
|
+
!popup ||
|
|
62
|
+
!popupId ||
|
|
63
|
+
event.origin !== globalThis.location.origin ||
|
|
64
|
+
event.source !== popup ||
|
|
65
|
+
completionStartedRef.current
|
|
66
|
+
) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const formPublicId = popupFormPublicIdRef.current;
|
|
71
|
+
const respondentCompletion =
|
|
72
|
+
formPublicId &&
|
|
73
|
+
formPublicId === formPublicIdRef.current &&
|
|
74
|
+
onRespondentAuthenticatedRef.current &&
|
|
75
|
+
isAuthPopupRespondentCompletionMessage(event.data, popupId, formPublicId)
|
|
76
|
+
? event.data
|
|
77
|
+
: null;
|
|
78
|
+
const accountCompletion = isAuthPopupCompletionMessage(event.data, popupId) ? event.data : null;
|
|
79
|
+
if (!respondentCompletion && !accountCompletion) return;
|
|
80
|
+
|
|
81
|
+
completionStartedRef.current = true;
|
|
82
|
+
|
|
83
|
+
/* Adopt only the identity kind returned by this still-active popup. */
|
|
84
|
+
const completeAuthentication = async () => {
|
|
85
|
+
if (respondentCompletion) {
|
|
86
|
+
await onRespondentAuthenticatedRef.current?.(respondentCompletion.respondentToken, respondentCompletion.respondentEmail);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const { auth, error } = await hydrateAuthenticationSession();
|
|
90
|
+
if (popupRef.current !== popup || popupIdRef.current !== popupId) return;
|
|
91
|
+
if (!auth?.token || !accountCompletion || !(await onAuthenticatedRef.current(auth, accountCompletion))) {
|
|
92
|
+
onErrorRef.current?.(error || { errorCode: 'network_error' });
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
void completeAuthentication()
|
|
96
|
+
.catch((error) => {
|
|
97
|
+
if (popupRef.current === popup) onErrorRef.current?.(error);
|
|
98
|
+
})
|
|
99
|
+
.finally(() => {
|
|
100
|
+
if (popupRef.current !== popup || popupIdRef.current !== popupId) return;
|
|
101
|
+
popup.close();
|
|
102
|
+
resetPopup();
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
globalThis.addEventListener('message', onMessage);
|
|
107
|
+
return () => {
|
|
108
|
+
globalThis.removeEventListener('message', onMessage);
|
|
109
|
+
popupRef.current?.close();
|
|
110
|
+
resetPopup();
|
|
111
|
+
};
|
|
112
|
+
}, [resetPopup]);
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
* Launch the configured same-origin sign-in route from a direct user action.
|
|
116
|
+
*/
|
|
117
|
+
const openAuthenticationPopup = useCallback(
|
|
118
|
+
(additionalSearchParams?: Record<string, string>) => {
|
|
119
|
+
if (!p.href || popupRef.current) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const popupUrl = new URL(p.href, globalThis.location.origin);
|
|
124
|
+
if (popupUrl.origin !== globalThis.location.origin) {
|
|
125
|
+
onErrorRef.current?.({ errorCode: 'forbidden' });
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const popupId = globalThis.crypto.randomUUID();
|
|
130
|
+
for (const [name, value] of Object.entries(additionalSearchParams || {})) {
|
|
131
|
+
popupUrl.searchParams.set(name, value);
|
|
132
|
+
}
|
|
133
|
+
popupUrl.searchParams.set('popup', '1');
|
|
134
|
+
popupUrl.searchParams.set('popupId', popupId);
|
|
135
|
+
const width = 520;
|
|
136
|
+
const height = 720;
|
|
137
|
+
const left = Math.max(0, globalThis.screenX + (globalThis.outerWidth - width) / 2);
|
|
138
|
+
const top = Math.max(0, globalThis.screenY + (globalThis.outerHeight - height) / 2);
|
|
139
|
+
const popup = globalThis.open(
|
|
140
|
+
popupUrl.toString(),
|
|
141
|
+
'marketday-auth',
|
|
142
|
+
`popup,width=${width},height=${height},left=${Math.round(left)},top=${Math.round(top)}`,
|
|
143
|
+
);
|
|
144
|
+
if (!popup) {
|
|
145
|
+
onErrorRef.current?.({ errorCode: 'popup_blocked' });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
popupRef.current = popup;
|
|
150
|
+
popupIdRef.current = popupId;
|
|
151
|
+
popupFormPublicIdRef.current = formPublicIdRef.current || null;
|
|
152
|
+
setPopupOpen(true);
|
|
153
|
+
popup.focus();
|
|
154
|
+
closeTimerRef.current = globalThis.setInterval(() => {
|
|
155
|
+
if (popup.closed && !completionStartedRef.current) {
|
|
156
|
+
resetPopup();
|
|
157
|
+
}
|
|
158
|
+
}, 500);
|
|
159
|
+
},
|
|
160
|
+
[p.href, resetPopup],
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
openAuthenticationPopup,
|
|
165
|
+
popupOpen,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
}
|