@jsb188/auth-web 1.1.11 → 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,16 +1,9 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
- import type {
3
- ServerErrorObj,
4
- SimpleErrorType,
5
- } from '@jsb188/app/types/app.d.ts';
2
+ import type { ServerErrorObj, SimpleErrorType } from '@jsb188/app/types/app.d.ts';
3
+ import { useIsLoggedIn } from '@jsb188/react/states';
6
4
  import type { ErrorMessageProps } from '@jsb188/react-web/modules/layout/MainLayout';
7
- import {
8
- AppContentArea,
9
- ErrorMessage,
10
- FullPageLayout,
11
- } from '@jsb188/react-web/modules/layout/MainLayout';
5
+ import { AppContentArea, ErrorMessage, FullPageLayout } from '@jsb188/react-web/modules/layout/MainLayout';
12
6
  import { BigLoading } from '@jsb188/react-web/ui/Loading';
13
- import { useIsLoggedIn } from '@jsb188/react/states';
14
7
 
15
8
  /**
16
9
  * Auth gate; loading
@@ -19,8 +12,8 @@ import { useIsLoggedIn } from '@jsb188/react/states';
19
12
  function AuthGateLoading({ message }: { message?: string }) {
20
13
  return (
21
14
  <>
22
- <BigLoading color="active" />
23
- {message && <p className="mt_df cl_md">{message}</p>}
15
+ <BigLoading color='active' />
16
+ {message && <p className='mt_df cl_md'>{message}</p>}
24
17
  </>
25
18
  );
26
19
  }
@@ -55,8 +48,7 @@ export function AuthGate(p: AuthGateProps) {
55
48
  ...other
56
49
  } = p;
57
50
  const isLoggedIn = useIsLoggedIn();
58
- const isNetworkError =
59
- (error as ServerErrorObj)?.errorCode === 'network_error';
51
+ const isNetworkError = (error as ServerErrorObj)?.errorCode === 'network_error';
60
52
  const isAuthLoading = loading && !isLoggedIn;
61
53
  const shouldRender = isLoggedIn && !notReady && !error;
62
54
 
@@ -68,7 +60,7 @@ export function AuthGate(p: AuthGateProps) {
68
60
  return (
69
61
  <>
70
62
  {appLoading && (
71
- <div className="abs_full v_center bg z6">
63
+ <div className='abs_full v_center bg z6'>
72
64
  <AuthGateLoading message={loadingMessage} />
73
65
  </div>
74
66
  )}
@@ -91,13 +83,6 @@ export function AuthGate(p: AuthGateProps) {
91
83
  );
92
84
  }
93
85
 
94
- if (!isLoggedIn && !isNetworkError) {
95
- // Gota do log in form here
96
- // Gota do log in form here
97
- // Gota do log in form here
98
- // Gota do log in form here
99
- }
100
-
101
86
  // IMPORTANT NOTE:
102
87
  // {connectedToServer} is currently always true, passed down from graphql hooks.
103
88
  // This is a placeholder logic, and must properly return a real network state in the future.
@@ -124,7 +109,7 @@ export function AuthGate(p: AuthGateProps) {
124
109
 
125
110
  if (notLayout) {
126
111
  return (
127
- <AppContentArea className="v_center pb_xl">
112
+ <AppContentArea className='v_center pb_xl'>
128
113
  <ErrorMessage {...errorProps} />
129
114
  </AppContentArea>
130
115
  );
@@ -0,0 +1,144 @@
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import { SMSCodeInput } from '@jsb188/form/modules/Form';
3
+ import { useConfirmPublicFormEmailCode, useSendPublicFormEmailCode } from '@jsb188/graphql/hooks/use-publicFormEmail-mtn';
4
+ import { ModalErrorMessage } from '@jsb188/react-web/ui/ModalUI';
5
+ import { useEffect, useState } from 'react';
6
+
7
+ /*
8
+ * Confirm an email code already sent by the public-form sign-in popup.
9
+ */
10
+ export const ConfirmPublicFormEmailCode = (p: {
11
+ email: string;
12
+ formPublicId: string;
13
+ onRespondentToken: (token: string, email: string | null) => boolean;
14
+ onChangeEmail: () => void;
15
+ }) => {
16
+ const [resendAt, setResendAt] = useState(() => Date.now() + 60_000);
17
+ const [resendSeconds, setResendSeconds] = useState(60);
18
+ const [codeInputVersion, setCodeInputVersion] = useState(0);
19
+ const {
20
+ confirmPublicFormEmailCode,
21
+ saving: confirming,
22
+ error: confirmError,
23
+ setError: setConfirmError,
24
+ resetErrors: resetConfirmErrors,
25
+ } = useConfirmPublicFormEmailCode({
26
+ /* Hand only the temporary respondent identity back to the current form. */
27
+ onCompleted: (data) => {
28
+ const auth = data?.confirmPublicFormEmailCode;
29
+ if (auth?.respondentToken && auth.respondentEmail) {
30
+ if (!p.onRespondentToken(auth.respondentToken, auth.respondentEmail)) {
31
+ setConfirmError({ message: i18n.t('error.network_error') });
32
+ }
33
+ } else {
34
+ setConfirmError({ message: i18n.t('error.20219') });
35
+ }
36
+ },
37
+ });
38
+ const {
39
+ sendPublicFormEmailCode,
40
+ saving: sending,
41
+ error: sendError,
42
+ setError: setSendError,
43
+ resetErrors: resetSendErrors,
44
+ } = useSendPublicFormEmailCode({
45
+ /* Clear the old code and restart the cooldown after replacement delivery is accepted. */
46
+ onCompleted: (data) => {
47
+ if (data?.sendPublicFormEmailCode) {
48
+ setCodeInputVersion((version) => version + 1);
49
+ setResendAt(Date.now() + 60_000);
50
+ setResendSeconds(60);
51
+ } else {
52
+ setSendError({ message: i18n.t('error.30000') });
53
+ }
54
+ },
55
+ });
56
+ const saving = sending || confirming;
57
+ const error = confirmError || sendError;
58
+
59
+ useEffect(() => {
60
+ if (!resendAt) {
61
+ return;
62
+ }
63
+
64
+ /* Derive the cooldown from the clock so background tabs do not prolong it. */
65
+ const updateCountdown = () => {
66
+ const seconds = Math.max(0, Math.ceil((resendAt - Date.now()) / 1000));
67
+ setResendSeconds(seconds);
68
+ if (!seconds) {
69
+ setResendAt(0);
70
+ }
71
+ };
72
+ const timer = globalThis.setInterval(updateCountdown, 500);
73
+ return () => globalThis.clearInterval(timer);
74
+ }, [resendAt]);
75
+
76
+ /* Clear errors before another request or returning to account discovery. */
77
+ const resetErrors = () => {
78
+ resetSendErrors();
79
+ resetConfirmErrors();
80
+ };
81
+
82
+ /* Reuse the SMS input's numeric code, including leading zeroes, for email verification. */
83
+ const onConfirmCode = (code: string) => {
84
+ if (saving) {
85
+ return;
86
+ }
87
+ resetErrors();
88
+ void confirmPublicFormEmailCode({ variables: { formPublicId: p.formPublicId, email: p.email, code } });
89
+ };
90
+
91
+ /* Request a replacement code only after the current cooldown has elapsed. */
92
+ const onResend = () => {
93
+ if (resendSeconds || saving) {
94
+ return;
95
+ }
96
+ resetErrors();
97
+ void sendPublicFormEmailCode({ variables: { formPublicId: p.formPublicId, email: p.email } });
98
+ };
99
+
100
+ /* Discover the replacement email's account before choosing password or code authentication. */
101
+ const onChangeEmail = () => {
102
+ resetErrors();
103
+ p.onChangeEmail();
104
+ };
105
+
106
+ return (
107
+ <>
108
+ {error ? (
109
+ <ModalErrorMessage
110
+ error={error}
111
+ resetErrors={resetErrors}
112
+ />
113
+ ) : null}
114
+ <SMSCodeInput
115
+ key={codeInputVersion}
116
+ codeLength={6}
117
+ saving={saving}
118
+ error={error}
119
+ onSubmit={onConfirmCode}
120
+ />
121
+ <p className='a_c p_n mt_md mb_df rel'>{i18n.t('auth.public_form_email_code_sent', { emailAddress: p.email })}</p>
122
+ <div className='v_center gap_df f_wrap'>
123
+ <button
124
+ type='button'
125
+ disabled={saving || !!resendSeconds}
126
+ onClick={onResend}
127
+ className='text ft_sm cl_md link'
128
+ >
129
+ {resendSeconds
130
+ ? i18n.t('auth.resend_verification_code_in', { seconds: resendSeconds })
131
+ : i18n.t('auth.resend_verification_code')}
132
+ </button>
133
+ <button
134
+ type='button'
135
+ disabled={saving}
136
+ onClick={onChangeEmail}
137
+ className='text ft_sm cl_md link'
138
+ >
139
+ {i18n.t('auth.use_different_email')}
140
+ </button>
141
+ </div>
142
+ </>
143
+ );
144
+ };