@jsb188/auth-web 1.1.10 → 1.1.11
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 +25 -25
- package/src/hooks/use-oauth.tsx +341 -311
- package/src/modules/AssignRequiredAuth.tsx +182 -170
- package/src/modules/AuthFormUI.tsx +28 -28
- package/src/modules/AuthForms-SSO.tsx +188 -151
- package/src/modules/AuthForms.tsx +676 -607
- package/src/modules/AuthGate.tsx +108 -85
- package/src/modules/InlineAuthForm.tsx +129 -116
- package/src/modules/ManagedSessions.tsx +109 -83
- package/src/modules/OtherAuthButtons.tsx +73 -71
- package/src/modules/_helpers.ts +102 -84
- package/src/modules/schemas.ts +231 -202
- package/src/schemas/account.ts +153 -138
- package/src/schemas/address.ts +155 -138
- package/src/ui/ManagedSessionUI.tsx +70 -64
|
@@ -1,109 +1,135 @@
|
|
|
1
1
|
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
2
|
import type { AccountSessionData } from '@jsb188/app/types/auth.d.ts';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
useRevokeMyAccountSession,
|
|
5
|
+
useRevokeMyOtherAccountSessions,
|
|
6
|
+
} from '@jsb188/graphql/hooks/use-auth-mtn';
|
|
4
7
|
import { useMyAccountSessions } from '@jsb188/graphql/hooks/use-auth-qry';
|
|
5
8
|
import { FullWidthButton } from '@jsb188/react-web/ui/Button';
|
|
6
9
|
import { BigLoading } from '@jsb188/react-web/ui/Loading';
|
|
7
10
|
import { useMemo, useState } from 'react';
|
|
8
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
type ManagedSessionListItem,
|
|
13
|
+
ManagedSessionListUI,
|
|
14
|
+
} from '../ui/ManagedSessionUI.tsx';
|
|
9
15
|
|
|
10
16
|
/*
|
|
11
17
|
* Format one managed-session timestamp for the current browser locale.
|
|
12
18
|
*/
|
|
13
19
|
function formatManagedSessionDate(value?: string | null) {
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
const date = value ? new Date(value) : null;
|
|
21
|
+
return date && !isNaN(date.getTime()) ? date.toLocaleString() : '-';
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
/*
|
|
19
25
|
* Convert server session rows into display-ready immutable list items.
|
|
20
26
|
*/
|
|
21
|
-
export function mapManagedSessionItems(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
export function mapManagedSessionItems(
|
|
28
|
+
sessions?: AccountSessionData[] | null,
|
|
29
|
+
): ManagedSessionListItem[] {
|
|
30
|
+
return (sessions || [])
|
|
31
|
+
.filter((session) => !session.revokedAt)
|
|
32
|
+
.map((session) => ({
|
|
33
|
+
...session,
|
|
34
|
+
displayName:
|
|
35
|
+
session.deviceName ||
|
|
36
|
+
session.userAgent ||
|
|
37
|
+
i18n.t('account.session_unknown_device'),
|
|
38
|
+
displayExpiresAt: formatManagedSessionDate(session.absoluteExpiresAt),
|
|
39
|
+
displayLastActivityAt: formatManagedSessionDate(session.lastActivityAt),
|
|
40
|
+
}))
|
|
41
|
+
.sort(
|
|
42
|
+
(a, b) =>
|
|
43
|
+
Number(b.current) - Number(a.current) ||
|
|
44
|
+
new Date(b.lastActivityAt).getTime() -
|
|
45
|
+
new Date(a.lastActivityAt).getTime(),
|
|
46
|
+
);
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
/*
|
|
34
50
|
* Load and manage the current account's active device sessions.
|
|
35
51
|
*/
|
|
36
52
|
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
|
-
|
|
53
|
+
const { accountSessions, loading } = useMyAccountSessions();
|
|
54
|
+
const [savingSessionId, setSavingSessionId] = useState<string | null>(null);
|
|
55
|
+
const [revokedSessionIds, setRevokedSessionIds] = useState<Set<string>>(
|
|
56
|
+
() => new Set(),
|
|
57
|
+
);
|
|
58
|
+
const [hideOtherSessions, setHideOtherSessions] = useState(false);
|
|
59
|
+
const { revokeMyAccountSession } = useRevokeMyAccountSession({
|
|
60
|
+
onCompleted: (data, _error, variables) => {
|
|
61
|
+
if (data?.revokeMyAccountSession && variables?.sessionId) {
|
|
62
|
+
setRevokedSessionIds((current) =>
|
|
63
|
+
new Set(current).add(variables.sessionId),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
setSavingSessionId(null);
|
|
67
|
+
},
|
|
68
|
+
onError: () => setSavingSessionId(null),
|
|
69
|
+
});
|
|
70
|
+
const { revokeMyOtherAccountSessions, saving: savingOthers } =
|
|
71
|
+
useRevokeMyOtherAccountSessions({
|
|
72
|
+
onCompleted: (data) => {
|
|
73
|
+
if (data?.revokeMyOtherAccountSessions) {
|
|
74
|
+
setHideOtherSessions(true);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
const items = useMemo(
|
|
79
|
+
() =>
|
|
80
|
+
mapManagedSessionItems(accountSessions).filter(
|
|
81
|
+
(session) =>
|
|
82
|
+
!revokedSessionIds.has(session.id) &&
|
|
83
|
+
(!hideOtherSessions || session.current),
|
|
84
|
+
),
|
|
85
|
+
[accountSessions, hideOtherSessions, revokedSessionIds],
|
|
86
|
+
);
|
|
61
87
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
88
|
+
/*
|
|
89
|
+
* Revoke one selected managed session.
|
|
90
|
+
*/
|
|
91
|
+
const onRevokeSession = (sessionId: string) => {
|
|
92
|
+
setSavingSessionId(sessionId);
|
|
93
|
+
revokeMyAccountSession({
|
|
94
|
+
variables: {
|
|
95
|
+
sessionId,
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
};
|
|
73
99
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
100
|
+
if (loading && !accountSessions) {
|
|
101
|
+
return (
|
|
102
|
+
<div className="h_160 v_center">
|
|
103
|
+
<BigLoading />
|
|
104
|
+
</div>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
81
107
|
|
|
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
|
-
|
|
108
|
+
return (
|
|
109
|
+
<div className="max_w_600">
|
|
110
|
+
<p className="cl_lt mb_sm">{i18n.t('account.sessions_desc')}</p>
|
|
111
|
+
<ManagedSessionListUI
|
|
112
|
+
items={items}
|
|
113
|
+
emptyText={i18n.t('account.session_none')}
|
|
114
|
+
revokeText={i18n.t('account.session_sign_out')}
|
|
115
|
+
currentText={i18n.t('account.session_current')}
|
|
116
|
+
expiresText={i18n.t('account.session_expires')}
|
|
117
|
+
lastActiveText={i18n.t('account.session_last_active')}
|
|
118
|
+
savingSessionId={savingSessionId}
|
|
119
|
+
onRevoke={onRevokeSession}
|
|
120
|
+
/>
|
|
121
|
+
{items.length <= 1 ? null : (
|
|
122
|
+
<FullWidthButton
|
|
123
|
+
fullWidth={false}
|
|
124
|
+
className="mt_md"
|
|
125
|
+
preset="outline"
|
|
126
|
+
loading={savingOthers}
|
|
127
|
+
disabled={savingOthers}
|
|
128
|
+
onClick={() => revokeMyOtherAccountSessions()}
|
|
129
|
+
>
|
|
130
|
+
{i18n.t('account.session_sign_out_others')}
|
|
131
|
+
</FullWidthButton>
|
|
132
|
+
)}
|
|
133
|
+
</div>
|
|
134
|
+
);
|
|
109
135
|
}
|
|
@@ -2,18 +2,18 @@ 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
9
|
|
|
10
10
|
interface OAuthButtonProps {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
formPublicId?: string | null;
|
|
12
|
+
onFinishSignIn: OnFinishSignInFn;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
interface AuthMethodButtonProps {
|
|
16
|
-
|
|
16
|
+
onClickButton: () => void;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -21,28 +21,29 @@ interface AuthMethodButtonProps {
|
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
export const ContinueWithGoogle = (p: OAuthButtonProps) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
const { onFinishSignIn, formPublicId } = p;
|
|
25
|
+
const { onInitiateGoogleSignIn, oauthButtonDisabled, waitingForOAuth } =
|
|
26
|
+
useContinueWithGoogle({
|
|
27
|
+
onFinishSignIn,
|
|
28
|
+
});
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
/* Launch Google OAuth with the current public-form context. */
|
|
31
|
+
const onClick = useCallback(() => {
|
|
32
|
+
onInitiateGoogleSignIn(undefined, null, formPublicId ?? null);
|
|
33
|
+
}, [formPublicId, onInitiateGoogleSignIn]);
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
35
|
+
return (
|
|
36
|
+
<FullWidthButton
|
|
37
|
+
disabled={oauthButtonDisabled}
|
|
38
|
+
loading={waitingForOAuth}
|
|
39
|
+
onClick={onClick}
|
|
40
|
+
preset="em"
|
|
41
|
+
className="mb_sm"
|
|
42
|
+
iconName="brand-google-color"
|
|
43
|
+
>
|
|
44
|
+
{i18n.t('auth.continue_with_google')}
|
|
45
|
+
</FullWidthButton>
|
|
46
|
+
);
|
|
46
47
|
};
|
|
47
48
|
|
|
48
49
|
/**
|
|
@@ -50,28 +51,29 @@ export const ContinueWithGoogle = (p: OAuthButtonProps) => {
|
|
|
50
51
|
*/
|
|
51
52
|
|
|
52
53
|
export const ContinueWithMicrosoft = (p: OAuthButtonProps) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
const { onFinishSignIn, formPublicId } = p;
|
|
55
|
+
const { onInitiateMicrosoftSignIn, oauthButtonDisabled, waitingForOAuth } =
|
|
56
|
+
useContinueWithMicrosoft({
|
|
57
|
+
onFinishSignIn,
|
|
58
|
+
});
|
|
57
59
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
/* Launch Microsoft OAuth with the current public-form context. */
|
|
61
|
+
const onClick = useCallback(() => {
|
|
62
|
+
onInitiateMicrosoftSignIn(undefined, undefined, formPublicId ?? null);
|
|
63
|
+
}, [formPublicId, onInitiateMicrosoftSignIn]);
|
|
62
64
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
65
|
+
return (
|
|
66
|
+
<FullWidthButton
|
|
67
|
+
disabled={oauthButtonDisabled}
|
|
68
|
+
loading={waitingForOAuth}
|
|
69
|
+
onClick={onClick}
|
|
70
|
+
preset="em"
|
|
71
|
+
className="mb_sm"
|
|
72
|
+
iconName="brand-microsoft-color"
|
|
73
|
+
>
|
|
74
|
+
{i18n.t('auth.continue_with_microsoft')}
|
|
75
|
+
</FullWidthButton>
|
|
76
|
+
);
|
|
75
77
|
};
|
|
76
78
|
|
|
77
79
|
/**
|
|
@@ -79,18 +81,18 @@ export const ContinueWithMicrosoft = (p: OAuthButtonProps) => {
|
|
|
79
81
|
*/
|
|
80
82
|
|
|
81
83
|
export const ContinueWithSso = (p: AuthMethodButtonProps) => {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
84
|
+
const { onClickButton } = p;
|
|
85
|
+
return (
|
|
86
|
+
<FullWidthButton
|
|
87
|
+
onClick={onClickButton}
|
|
88
|
+
preset="em"
|
|
89
|
+
className="mb_sm"
|
|
90
|
+
iconName="buildings-2-color"
|
|
91
|
+
iconClassName="ft_md shift_up"
|
|
92
|
+
>
|
|
93
|
+
{i18n.t('auth.continue_with_sso')}
|
|
94
|
+
</FullWidthButton>
|
|
95
|
+
);
|
|
94
96
|
};
|
|
95
97
|
|
|
96
98
|
/**
|
|
@@ -98,17 +100,17 @@ export const ContinueWithSso = (p: AuthMethodButtonProps) => {
|
|
|
98
100
|
*/
|
|
99
101
|
|
|
100
102
|
export const ContinueWithPhone = (p: AuthMethodButtonProps) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
103
|
+
const { onClickButton } = p;
|
|
104
|
+
return (
|
|
105
|
+
<FullWidthButton
|
|
106
|
+
onClick={onClickButton}
|
|
107
|
+
preset="em"
|
|
108
|
+
className="mb_sm"
|
|
109
|
+
iconName="phone-circle"
|
|
110
|
+
iconClassName="ft_md shift_up"
|
|
111
|
+
tryColor
|
|
112
|
+
>
|
|
113
|
+
{i18n.t('auth.continue_with_phone')}
|
|
114
|
+
</FullWidthButton>
|
|
115
|
+
);
|
|
114
116
|
};
|
package/src/modules/_helpers.ts
CHANGED
|
@@ -4,20 +4,26 @@ import { runWithAuthSessionBrowserLock } from '@jsb188/app/utils/authSession.ts'
|
|
|
4
4
|
* Types
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
export type AuthPageName =
|
|
7
|
+
export type AuthPageName =
|
|
8
|
+
| 'signin'
|
|
9
|
+
| 'signin_success'
|
|
10
|
+
| 'signup'
|
|
11
|
+
| 'signup_success'
|
|
12
|
+
| 'reset_password'
|
|
13
|
+
| 'welcome';
|
|
8
14
|
|
|
9
15
|
export type OAuthSessionAuthenticationInput = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
provider: 'GOOGLE' | 'MICROSOFT';
|
|
17
|
+
oauthToken?: string | null;
|
|
18
|
+
idToken?: string | null;
|
|
19
|
+
ssoConnectionId?: string | null;
|
|
20
|
+
formPublicId?: string | null;
|
|
21
|
+
authenticateOnly?: boolean;
|
|
16
22
|
};
|
|
17
23
|
|
|
18
24
|
export type OAuthSessionAuthenticationResult = {
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
auth: any | null;
|
|
26
|
+
error: any | null;
|
|
21
27
|
};
|
|
22
28
|
|
|
23
29
|
/**
|
|
@@ -25,43 +31,47 @@ export type OAuthSessionAuthenticationResult = {
|
|
|
25
31
|
*/
|
|
26
32
|
|
|
27
33
|
export function getAuthRoute(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
pageName: AuthPageName,
|
|
35
|
+
authRoutesMap?: Record<AuthPageName, string>,
|
|
36
|
+
queryStr?: string,
|
|
31
37
|
) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
const defaultAuthRoutesMap: Record<AuthPageName, string> = {
|
|
39
|
+
signin: '/auth/signin',
|
|
40
|
+
signin_success: '/auth/success',
|
|
41
|
+
signup: '/auth/signup',
|
|
42
|
+
signup_success: '/auth/signup-finished',
|
|
43
|
+
reset_password: '/auth/reset-password',
|
|
44
|
+
welcome: '/auth/welcome',
|
|
45
|
+
};
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
const pathname =
|
|
48
|
+
authRoutesMap?.[pageName] || defaultAuthRoutesMap[pageName] || 'auth';
|
|
49
|
+
return queryStr ? `${pathname}?${queryStr}` : pathname;
|
|
43
50
|
}
|
|
44
51
|
|
|
45
52
|
/*
|
|
46
53
|
* Post one login credential to the same-origin HttpOnly-cookie endpoint.
|
|
47
54
|
*/
|
|
48
|
-
async function postSignInSessionCredential(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
55
|
+
async function postSignInSessionCredential(
|
|
56
|
+
name: 'bootstrapToken' | 'token',
|
|
57
|
+
value: string,
|
|
58
|
+
): Promise<boolean> {
|
|
59
|
+
try {
|
|
60
|
+
const response = await fetch('/auth/session', {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
credentials: 'same-origin',
|
|
63
|
+
headers: {
|
|
64
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
65
|
+
},
|
|
66
|
+
body: new URLSearchParams({
|
|
67
|
+
[name]: value,
|
|
68
|
+
managedSessionVersion: '1',
|
|
69
|
+
}),
|
|
70
|
+
});
|
|
71
|
+
return response.ok;
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
65
75
|
}
|
|
66
76
|
|
|
67
77
|
/*
|
|
@@ -69,57 +79,65 @@ async function postSignInSessionCredential(name: 'bootstrapToken' | 'token', val
|
|
|
69
79
|
* atomically persist the resulting HttpOnly managed-session cookie.
|
|
70
80
|
*/
|
|
71
81
|
export async function authenticateOAuthSignInSession(
|
|
72
|
-
|
|
82
|
+
input: OAuthSessionAuthenticationInput,
|
|
73
83
|
): Promise<OAuthSessionAuthenticationResult> {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
if (typeof fetch !== 'function') {
|
|
85
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
86
|
+
}
|
|
77
87
|
|
|
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
|
-
|
|
88
|
+
try {
|
|
89
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
90
|
+
const body = new URLSearchParams({
|
|
91
|
+
provider: input.provider,
|
|
92
|
+
managedSessionVersion: '1',
|
|
93
|
+
...(input.oauthToken ? { oauthToken: input.oauthToken } : {}),
|
|
94
|
+
...(input.idToken ? { idToken: input.idToken } : {}),
|
|
95
|
+
...(input.ssoConnectionId
|
|
96
|
+
? { ssoConnectionId: input.ssoConnectionId }
|
|
97
|
+
: {}),
|
|
98
|
+
...(input.formPublicId ? { formPublicId: input.formPublicId } : {}),
|
|
99
|
+
...(input.authenticateOnly ? { authenticateOnly: '1' } : {}),
|
|
100
|
+
});
|
|
101
|
+
const response = await fetch('/auth/session', {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
credentials: 'same-origin',
|
|
104
|
+
headers: {
|
|
105
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
106
|
+
},
|
|
107
|
+
body,
|
|
108
|
+
});
|
|
109
|
+
const result = await response.json().catch(() => null);
|
|
110
|
+
if (!response.ok || !result?.ok) {
|
|
111
|
+
return {
|
|
112
|
+
auth: null,
|
|
113
|
+
error: result?.error || { errorCode: 'network_error' },
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
return { auth: result.auth || null, error: null };
|
|
117
|
+
});
|
|
118
|
+
} catch {
|
|
119
|
+
return { auth: null, error: { errorCode: 'network_error' } };
|
|
120
|
+
}
|
|
109
121
|
}
|
|
110
122
|
|
|
111
123
|
/*
|
|
112
124
|
* Persist an OAuth or phone login before navigating without a bearer-token query.
|
|
113
125
|
*/
|
|
114
|
-
export async function persistSignInSession(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
126
|
+
export async function persistSignInSession(
|
|
127
|
+
token: string,
|
|
128
|
+
bootstrapToken?: string | null,
|
|
129
|
+
): Promise<boolean> {
|
|
130
|
+
if (typeof fetch !== 'function' || (!token && !bootstrapToken)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
118
133
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
134
|
+
return await runWithAuthSessionBrowserLock(async () => {
|
|
135
|
+
if (
|
|
136
|
+
bootstrapToken &&
|
|
137
|
+
(await postSignInSessionCredential('bootstrapToken', bootstrapToken))
|
|
138
|
+
) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
return token ? await postSignInSessionCredential('token', token) : false;
|
|
142
|
+
});
|
|
125
143
|
}
|