@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.
- package/package.json +29 -0
- 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 +381 -0
- package/src/main.ts +21 -0
- package/src/modules/AssignRequiredAuth.tsx +274 -0
- package/src/modules/AuthFormUI.tsx +45 -0
- package/src/modules/AuthForms-SSO.tsx +209 -0
- package/src/modules/AuthForms.tsx +914 -0
- package/src/modules/AuthGate.tsx +123 -0
- package/src/modules/ConfirmPublicFormEmailCode.tsx +144 -0
- package/src/modules/InlineAuthForm.tsx +459 -0
- package/src/modules/ManagedSessions.tsx +115 -0
- package/src/modules/OtherAuthButtons.tsx +118 -0
- package/src/modules/_helpers.ts +200 -0
- package/src/modules/schemas.ts +177 -0
- package/src/schemas/account.ts +114 -0
- package/src/schemas/address.ts +133 -0
- package/src/schemas/index.ts +2 -0
- package/src/ui/ManagedSessionUI.tsx +95 -0
- package/src/utils/authPopup.ts +95 -0
- package/src/utils/googleIdentity.ts +100 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { cn } from '@jsb188/app/utils/string.ts';
|
|
3
|
+
import { Icon } from '@jsb188/react-web/svgs/Icon';
|
|
4
|
+
import { memo } from 'react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Auth form container
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
interface AuthFormContainerProps {
|
|
11
|
+
title?: string;
|
|
12
|
+
className?: string;
|
|
13
|
+
backHref?: string | null;
|
|
14
|
+
children: React.ReactNode;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const AuthFormContainer = memo((p: AuthFormContainerProps) => {
|
|
18
|
+
const { title, className, backHref, children } = p;
|
|
19
|
+
const goBackLabel = backHref ? i18n.t('c.go_back') : '';
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<div className="cw xs">
|
|
23
|
+
{!!backHref && (
|
|
24
|
+
<a
|
|
25
|
+
href={backHref}
|
|
26
|
+
aria-label={goBackLabel}
|
|
27
|
+
title={goBackLabel}
|
|
28
|
+
className="abs_tl_df av av_df r h_center bg cl_df bg_active_hv shadow_line z2 link"
|
|
29
|
+
>
|
|
30
|
+
<Icon name="arrow-left" />
|
|
31
|
+
</a>
|
|
32
|
+
)}
|
|
33
|
+
<div className="a_c -mb_5 anim_lift_scale_center on_mount spd_4">
|
|
34
|
+
<h1 className={cn("ft_lg ft_bold ls_1 lh_1 p_n cl_secondary op_50 visible", !title && 'mt_25')}>
|
|
35
|
+
{title}
|
|
36
|
+
</h1>
|
|
37
|
+
</div>
|
|
38
|
+
<div className={cn('bg px_md py_lg r_dfw rel z2', className)}>
|
|
39
|
+
{children}
|
|
40
|
+
</div>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
AuthFormContainer.displayName = 'AuthFormContainer';
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { useGetSsoLoginOptions } from '@jsb188/graphql/hooks/use-auth-mtn';
|
|
3
|
+
import SchemaForm from '@jsb188/form/libs/SchemaForm';
|
|
4
|
+
import { submissionResult } from '@jsb188/form/libs/validation';
|
|
5
|
+
import { FullWidthButton } from '@jsb188/react-web/ui/Button';
|
|
6
|
+
import { ModalErrorMessage } from '@jsb188/react-web/ui/ModalUI';
|
|
7
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
8
|
+
import { useNavigate } from 'react-router';
|
|
9
|
+
import type { OnFinishSignInFn, SsoLoginOptionData } from '../hooks/use-oauth';
|
|
10
|
+
import { useContinueWithGoogle, useContinueWithMicrosoft } from '../hooks/use-oauth';
|
|
11
|
+
import { getAuthReturnQuery, getAuthRoute, persistSignInSession } from './_helpers';
|
|
12
|
+
import { makeRequestTokenizedEmailSchema } from './schemas';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Types
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
interface SsoSignInProps {
|
|
19
|
+
authRoutesMap?: Record<string, string>;
|
|
20
|
+
formPublicId?: string | null;
|
|
21
|
+
returnTo?: string | null;
|
|
22
|
+
setAuthIface: (iface: null) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* SSO org picker; one button per matching org SSO connection, clicking it
|
|
27
|
+
* launches that org's provider flow (the click is the user gesture that is
|
|
28
|
+
* allowed to open the OAuth popup)
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
function SsoOrgOptions(p: {
|
|
32
|
+
email: string;
|
|
33
|
+
formPublicId?: string | null;
|
|
34
|
+
options: SsoLoginOptionData[];
|
|
35
|
+
onFinishSignIn: OnFinishSignInFn;
|
|
36
|
+
}) {
|
|
37
|
+
const { email, formPublicId, options, onFinishSignIn } = p;
|
|
38
|
+
const {
|
|
39
|
+
onInitiateGoogleSignIn,
|
|
40
|
+
oauthButtonDisabled: googleDisabled,
|
|
41
|
+
waitingForOAuth: waitingForGoogle,
|
|
42
|
+
} = useContinueWithGoogle({
|
|
43
|
+
onFinishSignIn,
|
|
44
|
+
});
|
|
45
|
+
const {
|
|
46
|
+
onInitiateMicrosoftSignIn,
|
|
47
|
+
oauthButtonDisabled: microsoftDisabled,
|
|
48
|
+
waitingForOAuth: waitingForMicrosoft,
|
|
49
|
+
} = useContinueWithMicrosoft({
|
|
50
|
+
onFinishSignIn,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const oauthButtonDisabled = googleDisabled || microsoftDisabled;
|
|
54
|
+
const waitingForOAuth = waitingForGoogle || waitingForMicrosoft;
|
|
55
|
+
|
|
56
|
+
/* Launch the provider flow associated with an organization SSO option. */
|
|
57
|
+
const onSelectOption = useCallback(
|
|
58
|
+
(option: SsoLoginOptionData) => {
|
|
59
|
+
switch (option.provider) {
|
|
60
|
+
case 'GOOGLE':
|
|
61
|
+
// "hint" pre-selects the typed email in Google's account chooser;
|
|
62
|
+
// the server validates the returned "hd" claim against the connection
|
|
63
|
+
onInitiateGoogleSignIn(
|
|
64
|
+
{
|
|
65
|
+
hint: email || undefined,
|
|
66
|
+
},
|
|
67
|
+
option.id,
|
|
68
|
+
formPublicId ?? null,
|
|
69
|
+
);
|
|
70
|
+
break;
|
|
71
|
+
case 'MICROSOFT':
|
|
72
|
+
onInitiateMicrosoftSignIn(option, email, formPublicId ?? null);
|
|
73
|
+
break;
|
|
74
|
+
default:
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
[email, formPublicId, onInitiateGoogleSignIn, onInitiateMicrosoftSignIn],
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<>
|
|
82
|
+
{options.map((option) => (
|
|
83
|
+
<FullWidthButton
|
|
84
|
+
key={option.id}
|
|
85
|
+
disabled={oauthButtonDisabled}
|
|
86
|
+
loading={waitingForOAuth}
|
|
87
|
+
onClick={() => onSelectOption(option)}
|
|
88
|
+
preset="em"
|
|
89
|
+
className="mb_sm"
|
|
90
|
+
iconName={option.provider === 'GOOGLE' ? 'brand-google-color' : 'brand-microsoft-color'}
|
|
91
|
+
>
|
|
92
|
+
{option.organizationName}
|
|
93
|
+
</FullWidthButton>
|
|
94
|
+
))}
|
|
95
|
+
|
|
96
|
+
<p className="a_c p_n mt_md mb_df rel">{i18n.t(options.length > 1 ? 'auth.sso_choose_org_instr' : 'auth.sso_continue_instr')}</p>
|
|
97
|
+
</>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* SSO sign in (email-first home realm discovery); asks for the email
|
|
103
|
+
* address, resolves which org SSO connections can sign it in, then routes
|
|
104
|
+
* to the matching provider flow
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
export function SsoSignIn(p: SsoSignInProps) {
|
|
108
|
+
const { authRoutesMap, formPublicId, returnTo, setAuthIface } = p;
|
|
109
|
+
const navigate = useNavigate();
|
|
110
|
+
const [email, setEmail] = useState('');
|
|
111
|
+
const [options, setOptions] = useState<SsoLoginOptionData[] | null>(null);
|
|
112
|
+
const emailSchema = useMemo(makeRequestTokenizedEmailSchema, []);
|
|
113
|
+
|
|
114
|
+
// Data hook
|
|
115
|
+
|
|
116
|
+
const { getSsoLoginOptions, saving, error, setError, resetErrors } = useGetSsoLoginOptions({
|
|
117
|
+
onCompleted: (data: any) => {
|
|
118
|
+
const list = data?.getSsoLoginOptions;
|
|
119
|
+
if (list?.length) {
|
|
120
|
+
setOptions(list);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
onError: (err: any) => {
|
|
124
|
+
setError(err);
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/* Persist the signed-in session before navigating to the success route. */
|
|
129
|
+
const onFinishSignIn = useCallback(
|
|
130
|
+
async (token: string, sessionBootstrapToken?: string | null, sessionPersisted?: boolean) => {
|
|
131
|
+
const persisted = sessionPersisted || (await persistSignInSession(token, sessionBootstrapToken));
|
|
132
|
+
if (!persisted) {
|
|
133
|
+
setError({
|
|
134
|
+
message: i18n.t('error.network_error'),
|
|
135
|
+
});
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
navigate(getAuthRoute('signin_success', authRoutesMap, getAuthReturnQuery(returnTo)));
|
|
139
|
+
return true;
|
|
140
|
+
},
|
|
141
|
+
[authRoutesMap, navigate, returnTo, setError],
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
/* Normalize the submitted email before resolving organization SSO options. */
|
|
145
|
+
const onSubmit = useCallback(
|
|
146
|
+
async (fv: { email: string }) => {
|
|
147
|
+
const nextEmail = fv.email?.trim().toLowerCase() || '';
|
|
148
|
+
setEmail(nextEmail);
|
|
149
|
+
const result = await getSsoLoginOptions({
|
|
150
|
+
variables: {
|
|
151
|
+
email: nextEmail,
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
return submissionResult(result);
|
|
155
|
+
},
|
|
156
|
+
[getSsoLoginOptions],
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
/* Return to the prior email or authentication-method selection step. */
|
|
160
|
+
const onBack = useCallback(() => {
|
|
161
|
+
if (options?.length) {
|
|
162
|
+
setOptions(null);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
setAuthIface(null);
|
|
166
|
+
}, [options, setAuthIface]);
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<>
|
|
170
|
+
{options?.length ? (
|
|
171
|
+
<SsoOrgOptions
|
|
172
|
+
email={email}
|
|
173
|
+
formPublicId={formPublicId}
|
|
174
|
+
options={options}
|
|
175
|
+
onFinishSignIn={onFinishSignIn}
|
|
176
|
+
/>
|
|
177
|
+
) : (
|
|
178
|
+
<SchemaForm
|
|
179
|
+
className="mb_df"
|
|
180
|
+
busy={saving}
|
|
181
|
+
definition={emailSchema}
|
|
182
|
+
onError={setError}
|
|
183
|
+
onSubmit={onSubmit}
|
|
184
|
+
buttonText={i18n.t('c.continue')}
|
|
185
|
+
>
|
|
186
|
+
{!error ? null : (
|
|
187
|
+
<ModalErrorMessage
|
|
188
|
+
error={error}
|
|
189
|
+
resetErrors={resetErrors}
|
|
190
|
+
/>
|
|
191
|
+
)}
|
|
192
|
+
|
|
193
|
+
<p className={error ? 'py_xs a_c' : '-mt_5 pb_xs a_c'}>{i18n.t('auth.sso_email_instr')}</p>
|
|
194
|
+
</SchemaForm>
|
|
195
|
+
)}
|
|
196
|
+
|
|
197
|
+
<div className="a_c h_center">
|
|
198
|
+
<button
|
|
199
|
+
type="button"
|
|
200
|
+
disabled={saving}
|
|
201
|
+
onClick={onBack}
|
|
202
|
+
className="text ft_sm cl_md link"
|
|
203
|
+
>
|
|
204
|
+
{i18n.t(options?.length ? 'auth.sso_use_different_email' : 'auth.sign_in_other_methods')}
|
|
205
|
+
</button>
|
|
206
|
+
</div>
|
|
207
|
+
</>
|
|
208
|
+
);
|
|
209
|
+
}
|