@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
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
2
|
import { useGetSsoLoginOptions } from '@jsb188/graphql/hooks/use-auth-mtn';
|
|
3
|
-
import SchemaForm from '@jsb188/
|
|
3
|
+
import SchemaForm from '@jsb188/form/libs/SchemaForm';
|
|
4
|
+
import { submissionResult } from '@jsb188/form/libs/validation';
|
|
4
5
|
import { FullWidthButton } from '@jsb188/react-web/ui/Button';
|
|
5
6
|
import { ModalErrorMessage } from '@jsb188/react-web/ui/ModalUI';
|
|
6
7
|
import { useCallback, useMemo, useState } from 'react';
|
|
7
8
|
import { useNavigate } from 'react-router';
|
|
8
9
|
import type { OnFinishSignInFn, SsoLoginOptionData } from '../hooks/use-oauth';
|
|
9
10
|
import { useContinueWithGoogle, useContinueWithMicrosoft } from '../hooks/use-oauth';
|
|
10
|
-
import { getAuthRoute, persistSignInSession } from './_helpers';
|
|
11
|
+
import { getAuthReturnQuery, getAuthRoute, persistSignInSession } from './_helpers';
|
|
11
12
|
import { makeRequestTokenizedEmailSchema } from './schemas';
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -15,8 +16,10 @@ import { makeRequestTokenizedEmailSchema } from './schemas';
|
|
|
15
16
|
*/
|
|
16
17
|
|
|
17
18
|
interface SsoSignInProps {
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
authRoutesMap?: Record<string, string>;
|
|
20
|
+
formPublicId?: string | null;
|
|
21
|
+
returnTo?: string | null;
|
|
22
|
+
setAuthIface: (iface: null) => void;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
/**
|
|
@@ -26,65 +29,73 @@ interface SsoSignInProps {
|
|
|
26
29
|
*/
|
|
27
30
|
|
|
28
31
|
function SsoOrgOptions(p: {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
email: string;
|
|
33
|
+
formPublicId?: string | null;
|
|
34
|
+
options: SsoLoginOptionData[];
|
|
35
|
+
onFinishSignIn: OnFinishSignInFn;
|
|
32
36
|
}) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
+
);
|
|
88
99
|
}
|
|
89
100
|
|
|
90
101
|
/**
|
|
@@ -94,94 +105,105 @@ function SsoOrgOptions(p: {
|
|
|
94
105
|
*/
|
|
95
106
|
|
|
96
107
|
export function SsoSignIn(p: SsoSignInProps) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
+
);
|
|
187
209
|
}
|