@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
|
@@ -6,7 +6,10 @@ import { ModalErrorMessage } from '@jsb188/react-web/ui/ModalUI';
|
|
|
6
6
|
import { useCallback, useMemo, useState } from 'react';
|
|
7
7
|
import { useNavigate } from 'react-router';
|
|
8
8
|
import type { OnFinishSignInFn, SsoLoginOptionData } from '../hooks/use-oauth';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
useContinueWithGoogle,
|
|
11
|
+
useContinueWithMicrosoft,
|
|
12
|
+
} from '../hooks/use-oauth';
|
|
10
13
|
import { getAuthRoute, persistSignInSession } from './_helpers';
|
|
11
14
|
import { makeRequestTokenizedEmailSchema } from './schemas';
|
|
12
15
|
|
|
@@ -15,8 +18,8 @@ import { makeRequestTokenizedEmailSchema } from './schemas';
|
|
|
15
18
|
*/
|
|
16
19
|
|
|
17
20
|
interface SsoSignInProps {
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
authRoutesMap?: Record<string, string>;
|
|
22
|
+
setAuthIface: (iface: null) => void;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
/**
|
|
@@ -26,65 +29,81 @@ interface SsoSignInProps {
|
|
|
26
29
|
*/
|
|
27
30
|
|
|
28
31
|
function SsoOrgOptions(p: {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
email: string;
|
|
33
|
+
options: SsoLoginOptionData[];
|
|
34
|
+
onFinishSignIn: OnFinishSignInFn;
|
|
32
35
|
}) {
|
|
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
|
-
|
|
36
|
+
const { email, options, onFinishSignIn } = p;
|
|
37
|
+
const {
|
|
38
|
+
onInitiateGoogleSignIn,
|
|
39
|
+
oauthButtonDisabled: googleDisabled,
|
|
40
|
+
waitingForOAuth: waitingForGoogle,
|
|
41
|
+
} = useContinueWithGoogle({
|
|
42
|
+
onFinishSignIn,
|
|
43
|
+
});
|
|
44
|
+
const {
|
|
45
|
+
onInitiateMicrosoftSignIn,
|
|
46
|
+
oauthButtonDisabled: microsoftDisabled,
|
|
47
|
+
waitingForOAuth: waitingForMicrosoft,
|
|
48
|
+
} = useContinueWithMicrosoft({
|
|
49
|
+
onFinishSignIn,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const oauthButtonDisabled = googleDisabled || microsoftDisabled;
|
|
53
|
+
const waitingForOAuth = waitingForGoogle || waitingForMicrosoft;
|
|
54
|
+
|
|
55
|
+
/* Launch the provider flow associated with an organization SSO option. */
|
|
56
|
+
const onSelectOption = useCallback(
|
|
57
|
+
(option: SsoLoginOptionData) => {
|
|
58
|
+
switch (option.provider) {
|
|
59
|
+
case 'GOOGLE':
|
|
60
|
+
// "hint" pre-selects the typed email in Google's account chooser;
|
|
61
|
+
// the server validates the returned "hd" claim against the connection
|
|
62
|
+
onInitiateGoogleSignIn(
|
|
63
|
+
{
|
|
64
|
+
hint: email || undefined,
|
|
65
|
+
},
|
|
66
|
+
option.id,
|
|
67
|
+
);
|
|
68
|
+
break;
|
|
69
|
+
case 'MICROSOFT':
|
|
70
|
+
onInitiateMicrosoftSignIn(option, email);
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
[email, onInitiateGoogleSignIn, onInitiateMicrosoftSignIn],
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<>
|
|
80
|
+
{options.map((option) => (
|
|
81
|
+
<FullWidthButton
|
|
82
|
+
key={option.id}
|
|
83
|
+
disabled={oauthButtonDisabled}
|
|
84
|
+
loading={waitingForOAuth}
|
|
85
|
+
onClick={() => onSelectOption(option)}
|
|
86
|
+
preset="em"
|
|
87
|
+
className="mb_sm"
|
|
88
|
+
iconName={
|
|
89
|
+
option.provider === 'GOOGLE'
|
|
90
|
+
? 'brand-google-color'
|
|
91
|
+
: 'brand-microsoft-color'
|
|
92
|
+
}
|
|
93
|
+
>
|
|
94
|
+
{option.organizationName}
|
|
95
|
+
</FullWidthButton>
|
|
96
|
+
))}
|
|
97
|
+
|
|
98
|
+
<p className="a_c p_n mt_md mb_df rel">
|
|
99
|
+
{i18n.t(
|
|
100
|
+
options.length > 1
|
|
101
|
+
? 'auth.sso_choose_org_instr'
|
|
102
|
+
: 'auth.sso_continue_instr',
|
|
103
|
+
)}
|
|
104
|
+
</p>
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
88
107
|
}
|
|
89
108
|
|
|
90
109
|
/**
|
|
@@ -94,94 +113,112 @@ function SsoOrgOptions(p: {
|
|
|
94
113
|
*/
|
|
95
114
|
|
|
96
115
|
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
|
-
|
|
116
|
+
const { authRoutesMap, setAuthIface } = p;
|
|
117
|
+
const navigate = useNavigate();
|
|
118
|
+
const [email, setEmail] = useState('');
|
|
119
|
+
const [options, setOptions] = useState<SsoLoginOptionData[] | null>(null);
|
|
120
|
+
const emailSchema = useMemo(makeRequestTokenizedEmailSchema, []);
|
|
121
|
+
|
|
122
|
+
// Data hook
|
|
123
|
+
|
|
124
|
+
const { getSsoLoginOptions, saving, error, setError, resetErrors } =
|
|
125
|
+
useGetSsoLoginOptions({
|
|
126
|
+
onCompleted: (data: any) => {
|
|
127
|
+
const list = data?.getSsoLoginOptions;
|
|
128
|
+
if (list?.length) {
|
|
129
|
+
setOptions(list);
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
onError: (err: any) => {
|
|
133
|
+
setError(err);
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
/* Persist the signed-in session before navigating to the success route. */
|
|
138
|
+
const onFinishSignIn = useCallback(
|
|
139
|
+
async (
|
|
140
|
+
token: string,
|
|
141
|
+
sessionBootstrapToken?: string | null,
|
|
142
|
+
sessionPersisted?: boolean,
|
|
143
|
+
) => {
|
|
144
|
+
const persisted =
|
|
145
|
+
sessionPersisted ||
|
|
146
|
+
(await persistSignInSession(token, sessionBootstrapToken));
|
|
147
|
+
if (!persisted) {
|
|
148
|
+
setError({
|
|
149
|
+
message: i18n.t('error.network_error'),
|
|
150
|
+
});
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
navigate(getAuthRoute('signin_success', authRoutesMap));
|
|
154
|
+
return true;
|
|
155
|
+
},
|
|
156
|
+
[authRoutesMap, navigate, setError],
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
/* Normalize the submitted email before resolving organization SSO options. */
|
|
160
|
+
const onSubmit = useCallback(
|
|
161
|
+
(fv: { email?: string | null }) => {
|
|
162
|
+
const nextEmail = fv.email?.trim().toLowerCase() || '';
|
|
163
|
+
setEmail(nextEmail);
|
|
164
|
+
getSsoLoginOptions({
|
|
165
|
+
variables: {
|
|
166
|
+
email: nextEmail,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
[getSsoLoginOptions],
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
/* Return to the prior email or authentication-method selection step. */
|
|
174
|
+
const onBack = useCallback(() => {
|
|
175
|
+
if (options?.length) {
|
|
176
|
+
setOptions(null);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
setAuthIface(null);
|
|
180
|
+
}, [options, setAuthIface]);
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
<>
|
|
184
|
+
{options?.length ? (
|
|
185
|
+
<SsoOrgOptions
|
|
186
|
+
email={email}
|
|
187
|
+
options={options}
|
|
188
|
+
onFinishSignIn={onFinishSignIn}
|
|
189
|
+
/>
|
|
190
|
+
) : (
|
|
191
|
+
<SchemaForm
|
|
192
|
+
className="mb_df"
|
|
193
|
+
saving={saving}
|
|
194
|
+
schema={emailSchema}
|
|
195
|
+
onError={setError}
|
|
196
|
+
onSubmit={onSubmit}
|
|
197
|
+
buttonText={i18n.t('form.continue')}
|
|
198
|
+
>
|
|
199
|
+
{!error ? null : (
|
|
200
|
+
<ModalErrorMessage error={error} resetErrors={resetErrors} />
|
|
201
|
+
)}
|
|
202
|
+
|
|
203
|
+
<p className={error ? 'py_xs a_c' : '-mt_5 pb_xs a_c'}>
|
|
204
|
+
{i18n.t('auth.sso_email_instr')}
|
|
205
|
+
</p>
|
|
206
|
+
</SchemaForm>
|
|
207
|
+
)}
|
|
208
|
+
|
|
209
|
+
<div className="a_c h_center">
|
|
210
|
+
<button
|
|
211
|
+
disabled={saving}
|
|
212
|
+
onClick={onBack}
|
|
213
|
+
className="text ft_sm cl_md link"
|
|
214
|
+
>
|
|
215
|
+
{i18n.t(
|
|
216
|
+
options?.length
|
|
217
|
+
? 'auth.sso_use_different_email'
|
|
218
|
+
: 'auth.sign_in_other_methods',
|
|
219
|
+
)}
|
|
220
|
+
</button>
|
|
221
|
+
</div>
|
|
222
|
+
</>
|
|
223
|
+
);
|
|
187
224
|
}
|