@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
|
@@ -11,99 +11,105 @@ import { type ManagedSessionListItem, ManagedSessionListUI } from '../ui/Managed
|
|
|
11
11
|
* Format one managed-session timestamp for the current browser locale.
|
|
12
12
|
*/
|
|
13
13
|
function formatManagedSessionDate(value?: string | null) {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const date = value ? new Date(value) : null;
|
|
15
|
+
return date && !isNaN(date.getTime()) ? date.toLocaleString() : '-';
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/*
|
|
19
19
|
* Convert server session rows into display-ready immutable list items.
|
|
20
20
|
*/
|
|
21
21
|
export function mapManagedSessionItems(sessions?: AccountSessionData[] | null): ManagedSessionListItem[] {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
if (!sessions?.length) return [];
|
|
23
|
+
return sessions
|
|
24
|
+
.filter((session) => !session.revokedAt)
|
|
25
|
+
.map((session) => ({
|
|
26
|
+
...session,
|
|
27
|
+
displayName: session.deviceName || session.userAgent || i18n.t('account.session_unknown_device'),
|
|
28
|
+
displayExpiresAt: formatManagedSessionDate(session.absoluteExpiresAt),
|
|
29
|
+
displayLastActivityAt: formatManagedSessionDate(session.lastActivityAt),
|
|
30
|
+
}))
|
|
31
|
+
.sort(
|
|
32
|
+
(a, b) => Number(b.current) - Number(a.current) || new Date(b.lastActivityAt).getTime() - new Date(a.lastActivityAt).getTime(),
|
|
33
|
+
);
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
/*
|
|
34
37
|
* Load and manage the current account's active device sessions.
|
|
35
38
|
*/
|
|
36
39
|
export function ManagedSessions() {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
40
|
+
const { accountSessions, loading } = useMyAccountSessions();
|
|
41
|
+
const [savingSessionId, setSavingSessionId] = useState<string | null>(null);
|
|
42
|
+
const [revokedSessionIds, setRevokedSessionIds] = useState<Set<string>>(() => new Set());
|
|
43
|
+
const [hideOtherSessions, setHideOtherSessions] = useState(false);
|
|
44
|
+
const { revokeMyAccountSession } = useRevokeMyAccountSession({
|
|
45
|
+
onCompleted: (data, _error, variables) => {
|
|
46
|
+
if (data?.revokeMyAccountSession && variables?.sessionId) {
|
|
47
|
+
setRevokedSessionIds((current) => new Set(current).add(variables.sessionId));
|
|
48
|
+
}
|
|
49
|
+
setSavingSessionId(null);
|
|
50
|
+
},
|
|
51
|
+
onError: () => setSavingSessionId(null),
|
|
52
|
+
});
|
|
53
|
+
const { revokeMyOtherAccountSessions, saving: savingOthers } = useRevokeMyOtherAccountSessions({
|
|
54
|
+
onCompleted: (data) => {
|
|
55
|
+
if (data?.revokeMyOtherAccountSessions) {
|
|
56
|
+
setHideOtherSessions(true);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const items = useMemo(
|
|
61
|
+
() =>
|
|
62
|
+
mapManagedSessionItems(accountSessions).filter(
|
|
63
|
+
(session) => !revokedSessionIds.has(session.id) && (!hideOtherSessions || session.current),
|
|
64
|
+
),
|
|
65
|
+
[accountSessions, hideOtherSessions, revokedSessionIds],
|
|
66
|
+
);
|
|
61
67
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
/*
|
|
69
|
+
* Revoke one selected managed session.
|
|
70
|
+
*/
|
|
71
|
+
const onRevokeSession = (sessionId: string) => {
|
|
72
|
+
setSavingSessionId(sessionId);
|
|
73
|
+
revokeMyAccountSession({
|
|
74
|
+
variables: {
|
|
75
|
+
sessionId,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
};
|
|
73
79
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
if (loading && !accountSessions) {
|
|
81
|
+
return (
|
|
82
|
+
<div className='h_160 v_center'>
|
|
83
|
+
<BigLoading />
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
81
87
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
88
|
+
return (
|
|
89
|
+
<div className='max_w_600'>
|
|
90
|
+
<p className='cl_lt mb_sm'>{i18n.t('account.sessions_desc')}</p>
|
|
91
|
+
<ManagedSessionListUI
|
|
92
|
+
items={items}
|
|
93
|
+
emptyText={i18n.t('account.session_none')}
|
|
94
|
+
revokeText={i18n.t('account.session_sign_out')}
|
|
95
|
+
currentText={i18n.t('account.session_current')}
|
|
96
|
+
expiresText={i18n.t('account.session_expires')}
|
|
97
|
+
lastActiveText={i18n.t('account.session_last_active')}
|
|
98
|
+
savingSessionId={savingSessionId}
|
|
99
|
+
onRevoke={onRevokeSession}
|
|
100
|
+
/>
|
|
101
|
+
{items.length <= 1 ? null : (
|
|
102
|
+
<FullWidthButton
|
|
103
|
+
fullWidth={false}
|
|
104
|
+
className='mt_md'
|
|
105
|
+
preset='outline'
|
|
106
|
+
loading={savingOthers}
|
|
107
|
+
disabled={savingOthers}
|
|
108
|
+
onClick={() => revokeMyOtherAccountSessions()}
|
|
109
|
+
>
|
|
110
|
+
{i18n.t('account.session_sign_out_others')}
|
|
111
|
+
</FullWidthButton>
|
|
112
|
+
)}
|
|
113
|
+
</div>
|
|
114
|
+
);
|
|
109
115
|
}
|
|
@@ -2,18 +2,19 @@ import i18n from '@jsb188/app/i18n/index.ts';
|
|
|
2
2
|
import { FullWidthButton } from '@jsb188/react-web/ui/Button';
|
|
3
3
|
import { useCallback } from 'react';
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
type OnFinishSignInFn,
|
|
6
|
+
useContinueWithGoogle,
|
|
7
|
+
useContinueWithMicrosoft,
|
|
8
8
|
} from '../hooks/use-oauth';
|
|
9
|
+
import { COMMON_ICON_NAMES } from '@jsb188/react-web/svgs/Icon';
|
|
9
10
|
|
|
10
11
|
interface OAuthButtonProps {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
formPublicId?: string | null;
|
|
13
|
+
onFinishSignIn: OnFinishSignInFn;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
interface AuthMethodButtonProps {
|
|
16
|
-
|
|
17
|
+
onClickButton: () => void;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
/**
|
|
@@ -21,28 +22,29 @@ interface AuthMethodButtonProps {
|
|
|
21
22
|
*/
|
|
22
23
|
|
|
23
24
|
export const ContinueWithGoogle = (p: OAuthButtonProps) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const { onFinishSignIn, formPublicId } = p;
|
|
26
|
+
const { onInitiateGoogleSignIn, oauthButtonDisabled, waitingForOAuth } =
|
|
27
|
+
useContinueWithGoogle({
|
|
28
|
+
onFinishSignIn,
|
|
29
|
+
});
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
/* Launch Google OAuth with the current public-form context. */
|
|
32
|
+
const onClick = useCallback(() => {
|
|
33
|
+
onInitiateGoogleSignIn(undefined, null, formPublicId ?? null);
|
|
34
|
+
}, [formPublicId, onInitiateGoogleSignIn]);
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
return (
|
|
37
|
+
<FullWidthButton
|
|
38
|
+
disabled={oauthButtonDisabled}
|
|
39
|
+
loading={waitingForOAuth}
|
|
40
|
+
onClick={onClick}
|
|
41
|
+
preset="em"
|
|
42
|
+
className="mb_sm"
|
|
43
|
+
iconName="brand-google-color"
|
|
44
|
+
>
|
|
45
|
+
{i18n.t('auth.continue_with_google')}
|
|
46
|
+
</FullWidthButton>
|
|
47
|
+
);
|
|
46
48
|
};
|
|
47
49
|
|
|
48
50
|
/**
|
|
@@ -50,28 +52,29 @@ export const ContinueWithGoogle = (p: OAuthButtonProps) => {
|
|
|
50
52
|
*/
|
|
51
53
|
|
|
52
54
|
export const ContinueWithMicrosoft = (p: OAuthButtonProps) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
const { onFinishSignIn, formPublicId } = p;
|
|
56
|
+
const { onInitiateMicrosoftSignIn, oauthButtonDisabled, waitingForOAuth } =
|
|
57
|
+
useContinueWithMicrosoft({
|
|
58
|
+
onFinishSignIn,
|
|
59
|
+
});
|
|
57
60
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
/* Launch Microsoft OAuth with the current public-form context. */
|
|
62
|
+
const onClick = useCallback(() => {
|
|
63
|
+
onInitiateMicrosoftSignIn(undefined, undefined, formPublicId ?? null);
|
|
64
|
+
}, [formPublicId, onInitiateMicrosoftSignIn]);
|
|
62
65
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
66
|
+
return (
|
|
67
|
+
<FullWidthButton
|
|
68
|
+
disabled={oauthButtonDisabled}
|
|
69
|
+
loading={waitingForOAuth}
|
|
70
|
+
onClick={onClick}
|
|
71
|
+
preset="em"
|
|
72
|
+
className="mb_sm"
|
|
73
|
+
iconName="brand-microsoft-color"
|
|
74
|
+
>
|
|
75
|
+
{i18n.t('auth.continue_with_microsoft')}
|
|
76
|
+
</FullWidthButton>
|
|
77
|
+
);
|
|
75
78
|
};
|
|
76
79
|
|
|
77
80
|
/**
|
|
@@ -79,18 +82,19 @@ export const ContinueWithMicrosoft = (p: OAuthButtonProps) => {
|
|
|
79
82
|
*/
|
|
80
83
|
|
|
81
84
|
export const ContinueWithSso = (p: AuthMethodButtonProps) => {
|
|
82
|
-
|
|
85
|
+
const { onClickButton } = p;
|
|
83
86
|
return (
|
|
84
87
|
<FullWidthButton
|
|
85
88
|
onClick={onClickButton}
|
|
86
|
-
preset=
|
|
87
|
-
className=
|
|
88
|
-
iconName=
|
|
89
|
-
iconClassName=
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
preset="em"
|
|
90
|
+
className="mb_sm"
|
|
91
|
+
iconName={COMMON_ICON_NAMES.enterprise_signin}
|
|
92
|
+
iconClassName="ft_md shift_up"
|
|
93
|
+
tryColor
|
|
94
|
+
>
|
|
95
|
+
{i18n.t('auth.continue_with_sso')}
|
|
96
|
+
</FullWidthButton>
|
|
97
|
+
);
|
|
94
98
|
};
|
|
95
99
|
|
|
96
100
|
/**
|
|
@@ -98,17 +102,17 @@ export const ContinueWithSso = (p: AuthMethodButtonProps) => {
|
|
|
98
102
|
*/
|
|
99
103
|
|
|
100
104
|
export const ContinueWithPhone = (p: AuthMethodButtonProps) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
105
|
+
const { onClickButton } = p;
|
|
106
|
+
return (
|
|
107
|
+
<FullWidthButton
|
|
108
|
+
onClick={onClickButton}
|
|
109
|
+
preset="em"
|
|
110
|
+
className="mb_sm"
|
|
111
|
+
iconName="phone-circle"
|
|
112
|
+
iconClassName="ft_md shift_up"
|
|
113
|
+
tryColor
|
|
114
|
+
>
|
|
115
|
+
{i18n.t('auth.continue_with_phone')}
|
|
116
|
+
</FullWidthButton>
|
|
117
|
+
);
|
|
114
118
|
};
|
package/src/modules/_helpers.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { runWithAuthSessionBrowserLock } from '@jsb188/app/utils/authSession.ts';
|
|
2
|
+
import { clearGoogleAutoSignInSuppression } from '../utils/googleIdentity';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Types
|
|
@@ -7,61 +8,96 @@ import { runWithAuthSessionBrowserLock } from '@jsb188/app/utils/authSession.ts'
|
|
|
7
8
|
export type AuthPageName = 'signin' | 'signin_success' | 'signup' | 'signup_success' | 'reset_password' | 'welcome';
|
|
8
9
|
|
|
9
10
|
export type OAuthSessionAuthenticationInput = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
provider: 'GOOGLE' | 'MICROSOFT';
|
|
12
|
+
oauthToken?: string | null;
|
|
13
|
+
idToken?: string | null;
|
|
14
|
+
ssoConnectionId?: string | null;
|
|
15
|
+
formPublicId?: string | null;
|
|
16
|
+
authenticateOnly?: boolean;
|
|
16
17
|
};
|
|
17
18
|
|
|
18
19
|
export type OAuthSessionAuthenticationResult = {
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
auth: any | null;
|
|
21
|
+
error: any | null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type AuthenticationSessionHydrationResult = {
|
|
25
|
+
auth: any | null;
|
|
26
|
+
error: any | null;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const DEFAULT_AUTH_ROUTES: Record<AuthPageName, string> = {
|
|
30
|
+
signin: '/auth/signin',
|
|
31
|
+
signin_success: '/auth/success',
|
|
32
|
+
signup: '/auth/signup',
|
|
33
|
+
signup_success: '/auth/signup-finished',
|
|
34
|
+
reset_password: '/auth/reset-password',
|
|
35
|
+
welcome: '/auth/welcome',
|
|
21
36
|
};
|
|
22
37
|
|
|
23
38
|
/**
|
|
24
39
|
* Helper; get route path
|
|
25
40
|
*/
|
|
26
41
|
|
|
27
|
-
export function getAuthRoute(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
export function getAuthRoute(pageName: AuthPageName, authRoutesMap?: Record<AuthPageName, string>, queryStr?: string) {
|
|
43
|
+
const pathname = authRoutesMap?.[pageName] || DEFAULT_AUTH_ROUTES[pageName] || 'auth';
|
|
44
|
+
return queryStr ? `${pathname}?${queryStr}` : pathname;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
* Encode one validated post-authentication destination for auth route links.
|
|
49
|
+
*/
|
|
50
|
+
export function getAuthReturnQuery(returnTo?: string | null) {
|
|
51
|
+
return returnTo ? new URLSearchParams({ returnTo }).toString() : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
* Accept only a same-site public-form destination and extract its campaign id.
|
|
56
|
+
*/
|
|
57
|
+
export function getPublicFormReturnContext(value?: string | null) {
|
|
58
|
+
const match = value?.match(/^\/form\/([A-Za-z0-9]+)$/);
|
|
59
|
+
return {
|
|
60
|
+
formPublicId: match?.[1] || null,
|
|
61
|
+
returnTo: match?.[0] || null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* Preserve only explicitly supported account-auth destinations. MCP consent is
|
|
67
|
+
* account authentication and must never acquire public-form respondent semantics.
|
|
68
|
+
*/
|
|
69
|
+
export function getAuthReturnContext(value?: string | null) {
|
|
70
|
+
const publicForm = getPublicFormReturnContext(value);
|
|
71
|
+
if (publicForm.returnTo) return publicForm;
|
|
72
|
+
const consent = value?.match(
|
|
73
|
+
/^\/auth\/mcp-consent\?request=([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$/,
|
|
74
|
+
);
|
|
75
|
+
return {
|
|
76
|
+
formPublicId: null,
|
|
77
|
+
returnTo: consent ? `/auth/mcp-consent?request=${consent[1]}` : value === '/auth/mcp-connections' ? value : null,
|
|
78
|
+
};
|
|
43
79
|
}
|
|
44
80
|
|
|
45
81
|
/*
|
|
46
82
|
* Post one login credential to the same-origin HttpOnly-cookie endpoint.
|
|
47
83
|
*/
|
|
48
84
|
async function postSignInSessionCredential(name: 'bootstrapToken' | 'token', value: string): Promise<boolean> {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
85
|
+
try {
|
|
86
|
+
const response = await fetch('/auth/session', {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
credentials: 'same-origin',
|
|
89
|
+
headers: {
|
|
90
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
91
|
+
},
|
|
92
|
+
body: new URLSearchParams({
|
|
93
|
+
[name]: value,
|
|
94
|
+
managedSessionVersion: '1',
|
|
95
|
+
}),
|
|
96
|
+
});
|
|
97
|
+
return response.ok;
|
|
98
|
+
} catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
65
101
|
}
|
|
66
102
|
|
|
67
103
|
/*
|
|
@@ -69,57 +105,96 @@ async function postSignInSessionCredential(name: 'bootstrapToken' | 'token', val
|
|
|
69
105
|
* atomically persist the resulting HttpOnly managed-session cookie.
|
|
70
106
|
*/
|
|
71
107
|
export async function authenticateOAuthSignInSession(
|
|
72
|
-
|
|
108
|
+
input: OAuthSessionAuthenticationInput,
|
|
109
|
+
signal?: AbortSignal,
|
|
73
110
|
): Promise<OAuthSessionAuthenticationResult> {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
111
|
+
if (typeof fetch !== 'function') {
|
|
112
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
117
|
+
signal?.throwIfAborted();
|
|
118
|
+
const body = new URLSearchParams({
|
|
119
|
+
provider: input.provider,
|
|
120
|
+
managedSessionVersion: '1',
|
|
121
|
+
...(input.oauthToken ? { oauthToken: input.oauthToken } : {}),
|
|
122
|
+
...(input.idToken ? { idToken: input.idToken } : {}),
|
|
123
|
+
...(input.ssoConnectionId ? { ssoConnectionId: input.ssoConnectionId } : {}),
|
|
124
|
+
...(input.formPublicId ? { formPublicId: input.formPublicId } : {}),
|
|
125
|
+
...(input.authenticateOnly ? { authenticateOnly: '1' } : {}),
|
|
126
|
+
});
|
|
127
|
+
const response = await fetch('/auth/session', {
|
|
128
|
+
signal,
|
|
129
|
+
method: 'POST',
|
|
130
|
+
credentials: 'same-origin',
|
|
131
|
+
headers: {
|
|
132
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
133
|
+
},
|
|
134
|
+
body,
|
|
135
|
+
});
|
|
136
|
+
const result = await response.json().catch(() => null);
|
|
137
|
+
if (!response.ok || !result?.ok) {
|
|
138
|
+
return {
|
|
139
|
+
auth: null,
|
|
140
|
+
error: result?.error || { errorCode: 'network_error' },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (input.provider === 'GOOGLE' && (result.auth?.account?.id || result.auth?.respondentToken)) {
|
|
144
|
+
clearGoogleAutoSignInSuppression();
|
|
145
|
+
}
|
|
146
|
+
return { auth: result.auth || null, error: null };
|
|
147
|
+
});
|
|
148
|
+
} catch {
|
|
149
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* Rehydrate browser authentication from the shared HttpOnly managed-session
|
|
155
|
+
* cookie after a same-origin popup establishes it.
|
|
156
|
+
*/
|
|
157
|
+
export async function hydrateAuthenticationSession(): Promise<AuthenticationSessionHydrationResult> {
|
|
158
|
+
if (typeof fetch !== 'function') {
|
|
159
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
164
|
+
const response = await fetch('/auth/session', {
|
|
165
|
+
method: 'POST',
|
|
166
|
+
credentials: 'same-origin',
|
|
167
|
+
headers: {
|
|
168
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
169
|
+
},
|
|
170
|
+
body: new URLSearchParams({ hydrate: '1' }),
|
|
171
|
+
});
|
|
172
|
+
const result = await response.json().catch(() => null);
|
|
173
|
+
if (!response.ok || !result?.ok || !result.auth?.token) {
|
|
174
|
+
return {
|
|
175
|
+
auth: null,
|
|
176
|
+
error: result?.error || { errorCode: 'network_error' },
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return { auth: result.auth, error: null };
|
|
180
|
+
});
|
|
181
|
+
} catch {
|
|
182
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
183
|
+
}
|
|
109
184
|
}
|
|
110
185
|
|
|
111
186
|
/*
|
|
112
187
|
* Persist an OAuth or phone login before navigating without a bearer-token query.
|
|
113
188
|
*/
|
|
114
189
|
export async function persistSignInSession(token: string, bootstrapToken?: string | null): Promise<boolean> {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
190
|
+
if (typeof fetch !== 'function' || (!token && !bootstrapToken)) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
195
|
+
if (bootstrapToken && (await postSignInSessionCredential('bootstrapToken', bootstrapToken))) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
return token ? await postSignInSessionCredential('token', token) : false;
|
|
199
|
+
});
|
|
125
200
|
}
|