@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/src/main.ts CHANGED
@@ -1,4 +1,14 @@
1
+ export * from './hooks/use-auth-popup';
1
2
  export * from './hooks/use-oauth';
3
+ export * from './hooks/use-google-one-tap';
4
+ export { suppressGoogleAutoSignIn } from './utils/googleIdentity';
5
+ export {
6
+ getAuthReturnQuery,
7
+ getAuthReturnContext,
8
+ getPublicFormReturnContext,
9
+ hydrateAuthenticationSession,
10
+ persistSignInSession,
11
+ } from './modules/_helpers';
2
12
  export * from './modules/AssignRequiredAuth';
3
13
  export * from './modules/AuthForms';
4
14
  export * from './modules/AuthFormUI';
@@ -8,3 +18,4 @@ export * from './modules/ManagedSessions';
8
18
  export * from './modules/OtherAuthButtons';
9
19
  export * from './modules/schemas';
10
20
  export * from './ui/ManagedSessionUI';
21
+ export * from './utils/authPopup';
@@ -1,13 +1,14 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
2
  import { useRequestChangeEmail } from '@jsb188/graphql/hooks/use-account-mtn';
3
- import { useChangeAccountPassword } from '@jsb188/graphql/hooks/use-auth-mtn';
3
+ import { useChangeAccountPassword, useRequestTokenizedEmail } from '@jsb188/graphql/hooks/use-auth-mtn';
4
+ import { AppContext } from '@jsb188/react/states';
4
5
  import { FullPageLayout } from '@jsb188/react-web/modules/layout/MainLayout';
5
- import SchemaForm from '@jsb188/react-web/modules/SchemaForm';
6
+ import SchemaForm from '@jsb188/form/libs/SchemaForm';
7
+ import { submissionResult } from '@jsb188/form/libs/validation';
6
8
  import { COMMON_ICON_NAMES } from '@jsb188/react-web/svgs/Icon';
7
9
  import { FullWidthButton } from '@jsb188/react-web/ui/Button';
8
10
  import Markdown from '@jsb188/react-web/ui/Markdown';
9
11
  import { ModalErrorMessage, ModalSimpleContent } from '@jsb188/react-web/ui/ModalUI';
10
- import { AppContext } from '@jsb188/react/states';
11
12
  import { useContext, useMemo, useState } from 'react';
12
13
  import { AuthFormContainer } from './AuthFormUI';
13
14
  import { makeAssignPasswordSchema, makeRequestTokenizedEmailSchema } from './schemas';
@@ -17,15 +18,16 @@ import { makeAssignPasswordSchema, makeRequestTokenizedEmailSchema } from './sch
17
18
  */
18
19
 
19
20
  interface FinishedResultObj {
20
- hasPassword: boolean;
21
- email: any;
22
- };
21
+ hasPassword: boolean;
22
+ email: any;
23
+ emailVerified: boolean;
24
+ }
23
25
 
24
26
  interface RequiredAuthSwitchCaseProps {
25
- identifier: string;
26
- onFinish: () => void;
27
- finishedResult: FinishedResultObj;
28
- setFinishedResult: any;
27
+ identifier: string;
28
+ onFinish: () => void;
29
+ finishedResult: FinishedResultObj;
30
+ setFinishedResult: any;
29
31
  }
30
32
 
31
33
  /**
@@ -33,25 +35,25 @@ interface RequiredAuthSwitchCaseProps {
33
35
  */
34
36
 
35
37
  function AccountSecuredMessage(p: RequiredAuthSwitchCaseProps) {
36
- const { onFinish } = p;
37
-
38
- return <ModalSimpleContent
39
- // title='Finished!'
40
- iconName={COMMON_ICON_NAMES.verified}
41
- className='px_df pt_md'
42
- >
43
- <Markdown as='p'>
44
- {i18n.t('auth.required_auth_fin_msg')}
45
- </Markdown>
46
-
47
- <FullWidthButton
48
- preset='bg_primary'
49
- className='mt_md shadow'
50
- onClick={onFinish}
51
- >
52
- {i18n.t('auth.continue_to_app')}
53
- </FullWidthButton>
54
- </ModalSimpleContent>;
38
+ const { onFinish } = p;
39
+
40
+ return (
41
+ <ModalSimpleContent
42
+ // title='Finished!'
43
+ iconName={COMMON_ICON_NAMES.verified}
44
+ className="px_df pt_md"
45
+ >
46
+ <Markdown as="p">{i18n.t('auth.required_auth_fin_msg')}</Markdown>
47
+
48
+ <FullWidthButton
49
+ preset="bg_primary"
50
+ className="mt_md shadow"
51
+ onClick={onFinish}
52
+ >
53
+ {i18n.t('auth.continue_to_app')}
54
+ </FullWidthButton>
55
+ </ModalSimpleContent>
56
+ );
55
57
  }
56
58
 
57
59
  /**
@@ -59,53 +61,53 @@ function AccountSecuredMessage(p: RequiredAuthSwitchCaseProps) {
59
61
  */
60
62
 
61
63
  function SetPasswordForm(p: RequiredAuthSwitchCaseProps) {
64
+ const { identifier, setFinishedResult } = p;
65
+ const assignPasswordSchema = useMemo(makeAssignPasswordSchema, []);
62
66
 
63
- const { identifier, setFinishedResult } = p;
64
- const assignPasswordSchema = useMemo(makeAssignPasswordSchema, []);
65
-
66
- const { changeAccountPassword, saving, error, setError, resetErrors } = useChangeAccountPassword({
67
- doNotFinishForm: true,
68
- onCompleted: (passwordChanged: boolean) => {
69
- if (passwordChanged) {
70
- setFinishedResult((fr: FinishedResultObj) => ({
71
- ...fr,
72
- hasPassword: true
73
- }));
74
- }
75
- }
76
- });
77
-
78
- const onSubmit = (fv: any) => {
79
- changeAccountPassword({
80
- variables: {
81
- currentPassword: '',
82
- password: fv.password,
83
- }
84
- });
85
- };
86
-
87
- return <>
88
- <p className='a_c mx_df mb_df -mt_5 pb_xs'>
89
- {i18n.t('auth.add_password_sign_in_msg', { identifier })}
90
- </p>
91
-
92
- <SchemaForm
93
- className='pb_df fs'
94
- saving={saving}
95
- schema={assignPasswordSchema}
96
- // currentData={{}}
97
- onError={setError}
98
- onSubmit={onSubmit}
99
- buttonText={i18n.t('auth.add_password')}
100
- >
101
- {!error ? null : (
102
- <ModalErrorMessage
103
- error={error}
104
- resetErrors={resetErrors}
105
- />
106
- )}
107
- </SchemaForm>
108
- </>;
67
+ const { changeAccountPassword, saving, error, setError, resetErrors } = useChangeAccountPassword({
68
+ doNotFinishForm: true,
69
+ onCompleted: (passwordChanged: boolean) => {
70
+ if (passwordChanged) {
71
+ setFinishedResult((fr: FinishedResultObj) => ({
72
+ ...fr,
73
+ hasPassword: true,
74
+ }));
75
+ }
76
+ },
77
+ });
78
+
79
+ /* Await the password mutation before completing this form step. */
80
+ const onSubmit = async (fv: { password: string; passwordRepeat: string }) => {
81
+ const result = await changeAccountPassword({
82
+ variables: {
83
+ currentPassword: '',
84
+ password: fv.password,
85
+ },
86
+ });
87
+ return submissionResult(result);
88
+ };
89
+
90
+ return (
91
+ <>
92
+ <p className="a_c mx_df mb_df -mt_5 pb_xs">{i18n.t('auth.add_password_sign_in_msg', { identifier })}</p>
93
+
94
+ <SchemaForm
95
+ className="pb_df fs"
96
+ busy={saving}
97
+ definition={assignPasswordSchema}
98
+ onError={setError}
99
+ onSubmit={onSubmit}
100
+ buttonText={i18n.t('auth.add_password')}
101
+ >
102
+ {!error ? null : (
103
+ <ModalErrorMessage
104
+ error={error}
105
+ resetErrors={resetErrors}
106
+ />
107
+ )}
108
+ </SchemaForm>
109
+ </>
110
+ );
109
111
  }
110
112
 
111
113
  /**
@@ -113,58 +115,98 @@ function SetPasswordForm(p: RequiredAuthSwitchCaseProps) {
113
115
  */
114
116
 
115
117
  function SetEmailForm(p: RequiredAuthSwitchCaseProps) {
118
+ const { finishedResult, identifier } = p;
119
+ const [waitingForVerification, setWaitingForVerification] = useState(false);
120
+ const assignPasswordSchema = useMemo(makeRequestTokenizedEmailSchema, []);
121
+ const existingEmailAddress = finishedResult.email?.address || '';
122
+ const currentData = useMemo(() => ({ email: existingEmailAddress }), [existingEmailAddress]);
123
+ const requiresExistingEmailVerification = !!existingEmailAddress && !finishedResult.emailVerified;
124
+
125
+ /*
126
+ * Show the check-email state after either email request succeeds.
127
+ */
128
+ const onEmailRequested = (result: any) => {
129
+ if (result?.requestChangeEmail || result?.requestTokenizedEmail) {
130
+ setWaitingForVerification(true);
131
+ }
132
+ };
116
133
 
117
- const { identifier } = p;
118
- const [waitingForVerification, setWaitingForVerification] = useState(false);
119
- const assignPasswordSchema = useMemo(makeRequestTokenizedEmailSchema, []);
120
-
121
- const { requestChangeEmail, error, setError, resetErrors, saving } = useRequestChangeEmail({
122
- onCompleted: (data: any) => {
123
- if (data.requestChangeEmail) {
124
- setWaitingForVerification(true);
125
- }
126
- }
127
- });
128
-
129
- const onSubmit = (fv: any) => {
130
- requestChangeEmail({
131
- variables: {
132
- emailAddress: fv.email,
133
- }
134
- });
135
- };
136
-
137
- if (waitingForVerification) {
138
- return <ModalSimpleContent
139
- className='px_df pt_md pb_25'
140
- iconName={COMMON_ICON_NAMES.email_address}
141
- title={i18n.t('auth.check_email')}
142
- message={i18n.t('auth.check_email_to_login_msg')}
143
- />;
144
- }
145
-
146
- return <>
147
- <p className='a_c mx_df mb_df -mt_5 pb_xs'>
148
- {i18n.t('auth.add_email_to_login_msg', { identifier })}
149
- </p>
150
-
151
- <SchemaForm
152
- className='pb_df fs'
153
- saving={saving}
154
- schema={assignPasswordSchema}
155
- // currentData={{}}
156
- onError={setError}
157
- onSubmit={onSubmit}
158
- buttonText={i18n.t('auth.add_email')}
159
- >
160
- {!error ? null : (
161
- <ModalErrorMessage
162
- error={error}
163
- resetErrors={resetErrors}
164
- />
165
- )}
166
- </SchemaForm>
167
- </>;
134
+ const changeEmail = useRequestChangeEmail();
135
+ const verifyEmail = useRequestTokenizedEmail();
136
+ const error = changeEmail.error || verifyEmail.error;
137
+ const saving = changeEmail.saving || verifyEmail.saving;
138
+
139
+ /*
140
+ * Clear errors from both possible email-request mutations.
141
+ */
142
+ const resetErrors = () => {
143
+ changeEmail.resetErrors();
144
+ verifyEmail.resetErrors();
145
+ };
146
+
147
+ /*
148
+ * Send a verification link for the current address, or a change-email link
149
+ * when the user supplies a different address.
150
+ */
151
+ const onSubmit = async (fv: { email: string }) => {
152
+ const emailAddress = fv.email.trim();
153
+ if (emailAddress.toLowerCase() === existingEmailAddress.trim().toLowerCase()) {
154
+ const result = await verifyEmail.requestTokenizedEmail({
155
+ variables: {
156
+ email: emailAddress,
157
+ requestType: 'EMAIL_VERIFICATION',
158
+ },
159
+ });
160
+ onEmailRequested(result);
161
+ return submissionResult(result);
162
+ }
163
+
164
+ const result = await changeEmail.requestChangeEmail({
165
+ variables: {
166
+ emailAddress,
167
+ },
168
+ });
169
+ onEmailRequested(result);
170
+ return submissionResult(result);
171
+ };
172
+
173
+ if (waitingForVerification) {
174
+ return (
175
+ <ModalSimpleContent
176
+ className="px_df pt_md pb_25"
177
+ iconName={COMMON_ICON_NAMES.email_address}
178
+ title={i18n.t('auth.check_email')}
179
+ message={i18n.t('auth.check_email_to_login_msg')}
180
+ />
181
+ );
182
+ }
183
+
184
+ return (
185
+ <>
186
+ <p className="a_c mx_df mb_df -mt_5 pb_xs">
187
+ {i18n.t(requiresExistingEmailVerification ? 'auth.verify_email_to_login_msg' : 'auth.add_email_to_login_msg', {
188
+ identifier,
189
+ })}
190
+ </p>
191
+
192
+ <SchemaForm
193
+ className="pb_df fs"
194
+ initialValues={currentData}
195
+ busy={saving}
196
+ definition={assignPasswordSchema}
197
+ onError={changeEmail.setError}
198
+ onSubmit={onSubmit}
199
+ buttonText={i18n.t(requiresExistingEmailVerification ? 'auth.resend_verification' : 'auth.add_email')}
200
+ >
201
+ {!error ? null : (
202
+ <ModalErrorMessage
203
+ error={error}
204
+ resetErrors={resetErrors}
205
+ />
206
+ )}
207
+ </SchemaForm>
208
+ </>
209
+ );
168
210
  }
169
211
 
170
212
  /**
@@ -172,57 +214,61 @@ function SetEmailForm(p: RequiredAuthSwitchCaseProps) {
172
214
  */
173
215
 
174
216
  function RequiredAuthSwitchCase(p: RequiredAuthSwitchCaseProps) {
175
- const { finishedResult } = p;
217
+ const { finishedResult } = p;
176
218
 
177
- if (!finishedResult.hasPassword) {
178
- return <SetPasswordForm {...p} />;
179
- } else if (!finishedResult.email) {
180
- // Keep email check last, becuase it has a verification step
181
- return <SetEmailForm {...p} />;
182
- }
219
+ if (!finishedResult.hasPassword) {
220
+ return <SetPasswordForm {...p} />;
221
+ }
222
+ if (!finishedResult.emailVerified) {
223
+ // Keep the email check last because it has a verification step.
224
+ return <SetEmailForm {...p} />;
225
+ }
183
226
 
184
- return <AccountSecuredMessage {...p} />;
227
+ return <AccountSecuredMessage {...p} />;
185
228
  }
186
229
 
187
230
  /**
188
231
  * Force users to provide required authentication info
189
232
  */
190
233
 
191
- export function AssignRequiredAuth(p: {
192
- hasPassword: boolean;
193
- hasEmail: boolean;
194
- }) {
195
- const { dispatchApp, appState: { hasPassword, account } } = useContext(AppContext);
196
- const identifier = account?.email?.address || account?.phone?.number || account?.id;
197
-
198
- const [finishedResult, setFinishedResult] = useState({
199
- hasPassword,
200
- email: account?.email?.address ? account?.email : null,
201
- });
202
-
203
- const onFinish = () => {
204
- if (!hasPassword) {
205
- dispatchApp({
206
- __type: 'APP_EDIT_AUTH',
207
- data: {
208
- id: account?.id,
209
- ...finishedResult
210
- }
211
- });
212
- }
213
- };
214
-
215
- return <FullPageLayout className='pattern_dot active_bf bg_fade'>
216
- <AuthFormContainer
217
- title={i18n.t('auth.one_last_step_')}
218
- className='bd_5 bd_main'
219
- >
220
- <RequiredAuthSwitchCase
221
- identifier={identifier!}
222
- finishedResult={finishedResult}
223
- setFinishedResult={setFinishedResult}
224
- onFinish={onFinish}
225
- />
226
- </AuthFormContainer>
227
- </FullPageLayout>;
234
+ export function AssignRequiredAuth(p: { hasPassword: boolean; hasVerifiedEmail: boolean }) {
235
+ const {
236
+ dispatchApp,
237
+ appState: { hasPassword, account },
238
+ } = useContext(AppContext);
239
+ const identifier = account?.email?.address || account?.phone?.number || account?.id;
240
+
241
+ const [finishedResult, setFinishedResult] = useState({
242
+ hasPassword,
243
+ email: account?.email?.address ? account?.email : null,
244
+ emailVerified: p.hasVerifiedEmail,
245
+ });
246
+
247
+ const onFinish = () => {
248
+ if (!hasPassword) {
249
+ dispatchApp({
250
+ __type: 'APP_EDIT_AUTH',
251
+ data: {
252
+ id: account?.id,
253
+ ...finishedResult,
254
+ },
255
+ });
256
+ }
257
+ };
258
+
259
+ return (
260
+ <FullPageLayout className="pattern_dot active_bf bg_fade">
261
+ <AuthFormContainer
262
+ title={i18n.t('auth.one_last_step_')}
263
+ className="bd_5 bd_main"
264
+ >
265
+ <RequiredAuthSwitchCase
266
+ identifier={identifier!}
267
+ finishedResult={finishedResult}
268
+ setFinishedResult={setFinishedResult}
269
+ onFinish={onFinish}
270
+ />
271
+ </AuthFormContainer>
272
+ </FullPageLayout>
273
+ );
228
274
  }
@@ -8,38 +8,38 @@ import { memo } from 'react';
8
8
  */
9
9
 
10
10
  interface AuthFormContainerProps {
11
- title?: string;
12
- className?: string;
13
- backHref?: string | null;
14
- children: React.ReactNode;
11
+ title?: string;
12
+ className?: string;
13
+ backHref?: string | null;
14
+ children: React.ReactNode;
15
15
  }
16
16
 
17
17
  export const AuthFormContainer = memo((p: AuthFormContainerProps) => {
18
- const { title, className, backHref, children } = p;
19
- const goBackLabel = backHref ? i18n.t('form.go_back') : '';
18
+ const { title, className, backHref, children } = p;
19
+ const goBackLabel = backHref ? i18n.t('c.go_back') : '';
20
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='ft_lg ft_bold ls_1 lh_1 p_n cl_secondary op_50 visible'>
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
- );
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
43
  });
44
44
 
45
45
  AuthFormContainer.displayName = 'AuthFormContainer';