@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.
@@ -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 { useContinueWithGoogle, useContinueWithMicrosoft } from '../hooks/use-oauth';
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
- authRoutesMap?: Record<string, string>;
19
- setAuthIface: (iface: null) => void;
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
- email: string;
30
- options: SsoLoginOptionData[];
31
- onFinishSignIn: OnFinishSignInFn;
32
+ email: string;
33
+ options: SsoLoginOptionData[];
34
+ onFinishSignIn: OnFinishSignInFn;
32
35
  }) {
33
- const { email, options, onFinishSignIn } = p;
34
- const {
35
- onInitiateGoogleSignIn,
36
- oauthButtonDisabled: googleDisabled,
37
- waitingForOAuth: waitingForGoogle,
38
- } = useContinueWithGoogle({
39
- onFinishSignIn,
40
- });
41
- const {
42
- onInitiateMicrosoftSignIn,
43
- oauthButtonDisabled: microsoftDisabled,
44
- waitingForOAuth: waitingForMicrosoft,
45
- } = useContinueWithMicrosoft({
46
- onFinishSignIn,
47
- });
48
-
49
- const oauthButtonDisabled = googleDisabled || microsoftDisabled;
50
- const waitingForOAuth = waitingForGoogle || waitingForMicrosoft;
51
-
52
- /* Launch the provider flow associated with an organization SSO option. */
53
- const onSelectOption = useCallback((option: SsoLoginOptionData) => {
54
- switch (option.provider) {
55
- case 'GOOGLE':
56
- // "hint" pre-selects the typed email in Google's account chooser;
57
- // the server validates the returned "hd" claim against the connection
58
- onInitiateGoogleSignIn({
59
- hint: email || undefined,
60
- }, option.id);
61
- break;
62
- case 'MICROSOFT':
63
- onInitiateMicrosoftSignIn(option, email);
64
- break;
65
- default:
66
- }
67
- }, [email, onInitiateGoogleSignIn, onInitiateMicrosoftSignIn]);
68
-
69
- return <>
70
- {options.map((option) => (
71
- <FullWidthButton
72
- key={option.id}
73
- disabled={oauthButtonDisabled}
74
- loading={waitingForOAuth}
75
- onClick={() => onSelectOption(option)}
76
- preset='em'
77
- className='mb_sm'
78
- iconName={option.provider === 'GOOGLE' ? 'brand-google-color' : 'brand-microsoft-color'}
79
- >
80
- {option.organizationName}
81
- </FullWidthButton>
82
- ))}
83
-
84
- <p className='a_c p_n mt_md mb_df rel'>
85
- {i18n.t(options.length > 1 ? 'auth.sso_choose_org_instr' : 'auth.sso_continue_instr')}
86
- </p>
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
- const { authRoutesMap, setAuthIface } = p;
98
- const navigate = useNavigate();
99
- const [email, setEmail] = useState('');
100
- const [options, setOptions] = useState<SsoLoginOptionData[] | null>(null);
101
- const emailSchema = useMemo(makeRequestTokenizedEmailSchema, []);
102
-
103
- // Data hook
104
-
105
- const { getSsoLoginOptions, saving, error, setError, resetErrors } = useGetSsoLoginOptions({
106
- onCompleted: (data: any) => {
107
- const list = data?.getSsoLoginOptions;
108
- if (list?.length) {
109
- setOptions(list);
110
- }
111
- },
112
- onError: (err: any) => {
113
- setError(err);
114
- },
115
- });
116
-
117
- /* Persist the signed-in session before navigating to the success route. */
118
- const onFinishSignIn = useCallback(async (token: string, sessionBootstrapToken?: string | null, sessionPersisted?: boolean) => {
119
- const persisted = sessionPersisted || await persistSignInSession(token, sessionBootstrapToken);
120
- if (!persisted) {
121
- setError({
122
- message: i18n.t('error.network_error'),
123
- });
124
- return false;
125
- }
126
- navigate(getAuthRoute('signin_success', authRoutesMap));
127
- return true;
128
- }, [authRoutesMap, navigate, setError]);
129
-
130
- /* Normalize the submitted email before resolving organization SSO options. */
131
- const onSubmit = useCallback((fv: { email?: string | null }) => {
132
- const nextEmail = fv.email?.trim().toLowerCase() || '';
133
- setEmail(nextEmail);
134
- getSsoLoginOptions({
135
- variables: {
136
- email: nextEmail,
137
- },
138
- });
139
- }, [getSsoLoginOptions]);
140
-
141
- /* Return to the prior email or authentication-method selection step. */
142
- const onBack = useCallback(() => {
143
- if (options?.length) {
144
- setOptions(null);
145
- return;
146
- }
147
- setAuthIface(null);
148
- }, [options, setAuthIface]);
149
-
150
- return <>
151
- {options?.length
152
- ? <SsoOrgOptions
153
- email={email}
154
- options={options}
155
- onFinishSignIn={onFinishSignIn}
156
- />
157
- : <SchemaForm
158
- className='mb_df'
159
- saving={saving}
160
- schema={emailSchema}
161
- onError={setError}
162
- onSubmit={onSubmit}
163
- buttonText={i18n.t('form.continue')}
164
- >
165
- {!error ? null : (
166
- <ModalErrorMessage
167
- error={error}
168
- resetErrors={resetErrors}
169
- />
170
- )}
171
-
172
- <p className={error ? 'py_xs a_c' : '-mt_5 pb_xs a_c'}>
173
- {i18n.t('auth.sso_email_instr')}
174
- </p>
175
- </SchemaForm>}
176
-
177
- <div className='a_c h_center'>
178
- <button
179
- disabled={saving}
180
- onClick={onBack}
181
- className='text ft_sm cl_md link'
182
- >
183
- {i18n.t(options?.length ? 'auth.sso_use_different_email' : 'auth.sign_in_other_methods')}
184
- </button>
185
- </div>
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
  }