@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.
@@ -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/react-web/modules/SchemaForm';
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
- authRoutesMap?: Record<string, string>;
19
- setAuthIface: (iface: null) => void;
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
- email: string;
30
- options: SsoLoginOptionData[];
31
- onFinishSignIn: OnFinishSignInFn;
32
+ email: string;
33
+ formPublicId?: string | null;
34
+ options: SsoLoginOptionData[];
35
+ onFinishSignIn: OnFinishSignInFn;
32
36
  }) {
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
- </>;
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
- 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
- </>;
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
  }