@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.
@@ -0,0 +1,95 @@
1
+ import type { AccountSessionData } from '@jsb188/app/types/auth.d.ts';
2
+ import { cn } from '@jsb188/app/utils/string.ts';
3
+ import { Button } from '@jsb188/react-web/ui/Button';
4
+ import { memo } from 'react';
5
+
6
+ export type ManagedSessionListItem = AccountSessionData & {
7
+ displayExpiresAt: string;
8
+ displayLastActivityAt: string;
9
+ displayName: string;
10
+ };
11
+
12
+ /*
13
+ * Render one managed login session without owning data or interaction state.
14
+ */
15
+ const ManagedSessionRowUI = memo(
16
+ (p: {
17
+ item: ManagedSessionListItem;
18
+ revokeText: string;
19
+ currentText: string;
20
+ expiresText: string;
21
+ lastActiveText: string;
22
+ saving: boolean;
23
+ onRevoke: (sessionId: string) => void;
24
+ }) => {
25
+ const { item } = p;
26
+
27
+ return (
28
+ <div className="h_spread gap_df py_12 bd_b_1 bd_lt">
29
+ <div className="min_w_0">
30
+ <div className="h_item gap_8">
31
+ <strong className="ft_medium ellipsis">{item.displayName}</strong>
32
+ {!item.current ? null : (
33
+ <span className="ft_xs cl_active">{p.currentText}</span>
34
+ )}
35
+ </div>
36
+ <p className="ft_sm cl_lt ellipsis">
37
+ {p.lastActiveText.replace('%{date}', item.displayLastActivityAt)}
38
+ </p>
39
+ <p className="ft_xs cl_lt ellipsis">
40
+ {p.expiresText.replace('%{date}', item.displayExpiresAt)}
41
+ </p>
42
+ </div>
43
+ <Button
44
+ size="sm"
45
+ preset={item.current ? 'cl_err' : 'outline'}
46
+ className={cn('shrink_0', p.saving && 'disabled')}
47
+ disabled={p.saving}
48
+ text={p.revokeText}
49
+ onClick={() => p.onRevoke(item.id)}
50
+ />
51
+ </div>
52
+ );
53
+ },
54
+ );
55
+
56
+ ManagedSessionRowUI.displayName = 'ManagedSessionRowUI';
57
+
58
+ /*
59
+ * Render a presentation-only list of managed login sessions.
60
+ */
61
+ export const ManagedSessionListUI = memo(
62
+ (p: {
63
+ items: ManagedSessionListItem[];
64
+ emptyText: string;
65
+ revokeText: string;
66
+ currentText: string;
67
+ expiresText: string;
68
+ lastActiveText: string;
69
+ savingSessionId?: string | null;
70
+ onRevoke: (sessionId: string) => void;
71
+ }) => {
72
+ if (!p.items.length) {
73
+ return <p className="cl_lt py_md">{p.emptyText}</p>;
74
+ }
75
+
76
+ return (
77
+ <div>
78
+ {p.items.map((item) => (
79
+ <ManagedSessionRowUI
80
+ key={item.id}
81
+ item={item}
82
+ revokeText={p.revokeText}
83
+ currentText={p.currentText}
84
+ expiresText={p.expiresText}
85
+ lastActiveText={p.lastActiveText}
86
+ saving={p.savingSessionId === item.id}
87
+ onRevoke={p.onRevoke}
88
+ />
89
+ ))}
90
+ </div>
91
+ );
92
+ },
93
+ );
94
+
95
+ ManagedSessionListUI.displayName = 'ManagedSessionListUI';
@@ -0,0 +1,95 @@
1
+ export const AUTH_POPUP_MESSAGE_TYPE = 'marketday-auth-complete';
2
+
3
+ export interface AuthPopupCompletionMessage {
4
+ popupId: string;
5
+ primaryOrganizationId?: string;
6
+ type: typeof AUTH_POPUP_MESSAGE_TYPE;
7
+ }
8
+
9
+ /*
10
+ * Return the completion route for one valid UUID-bound authentication popup.
11
+ */
12
+ export function getAuthPopupCompletionPath(popupId?: string | null): string | null {
13
+ if (!popupId || !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(popupId)) {
14
+ return null;
15
+ }
16
+
17
+ return `/auth/popup-complete?${new URLSearchParams({ popupId })}`;
18
+ }
19
+
20
+ /*
21
+ * Return whether a cross-window message completes the currently open
22
+ * authentication popup.
23
+ */
24
+ export function isAuthPopupCompletionMessage(value: unknown, popupId: string): value is AuthPopupCompletionMessage {
25
+ if (!value || typeof value !== 'object') {
26
+ return false;
27
+ }
28
+
29
+ const message = value as Partial<AuthPopupCompletionMessage>;
30
+ return (
31
+ message.type === AUTH_POPUP_MESSAGE_TYPE &&
32
+ message.popupId === popupId &&
33
+ (message.primaryOrganizationId === undefined || typeof message.primaryOrganizationId === 'string')
34
+ );
35
+ }
36
+
37
+ export const AUTH_POPUP_RESPONDENT_MESSAGE_TYPE = 'marketday-form-auth-complete';
38
+
39
+ export interface AuthPopupRespondentCompletionMessage {
40
+ type: typeof AUTH_POPUP_RESPONDENT_MESSAGE_TYPE;
41
+ popupId: string;
42
+ formPublicId: string;
43
+ respondentToken: string;
44
+ respondentEmail: string;
45
+ }
46
+
47
+ /*
48
+ * Accept a respondent credential only for the popup and public form that requested it.
49
+ */
50
+ export function isAuthPopupRespondentCompletionMessage(
51
+ value: unknown,
52
+ popupId: string,
53
+ formPublicId: string,
54
+ ): value is AuthPopupRespondentCompletionMessage {
55
+ if (!value || typeof value !== 'object') return false;
56
+ const message = value as Partial<AuthPopupRespondentCompletionMessage>;
57
+ return (
58
+ message.type === AUTH_POPUP_RESPONDENT_MESSAGE_TYPE &&
59
+ message.popupId === popupId &&
60
+ message.formPublicId === formPublicId &&
61
+ typeof message.respondentToken === 'string' &&
62
+ !!message.respondentToken &&
63
+ typeof message.respondentEmail === 'string' &&
64
+ !!message.respondentEmail
65
+ );
66
+ }
67
+
68
+ /*
69
+ * Hand a verified form identity directly to the same-origin opener without putting credentials in a URL or cookie.
70
+ */
71
+ export function postAuthPopupRespondentCompletion(
72
+ popupId: string,
73
+ formPublicId: string,
74
+ respondentToken: string,
75
+ respondentEmail: string,
76
+ ): boolean {
77
+ if (!getAuthPopupCompletionPath(popupId) || !formPublicId || !respondentToken || !respondentEmail || !globalThis.opener) {
78
+ return false;
79
+ }
80
+ try {
81
+ globalThis.opener.postMessage(
82
+ {
83
+ type: AUTH_POPUP_RESPONDENT_MESSAGE_TYPE,
84
+ popupId,
85
+ formPublicId,
86
+ respondentToken,
87
+ respondentEmail,
88
+ } satisfies AuthPopupRespondentCompletionMessage,
89
+ globalThis.location.origin,
90
+ );
91
+ return true;
92
+ } catch {
93
+ return false;
94
+ }
95
+ }
@@ -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
+ }