@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,23 +1,21 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
2
  import type { ServerErrorObj, SimpleErrorType } from '@jsb188/app/types/app.d.ts';
3
+ import { useIsLoggedIn } from '@jsb188/react/states';
3
4
  import type { ErrorMessageProps } from '@jsb188/react-web/modules/layout/MainLayout';
4
5
  import { AppContentArea, ErrorMessage, FullPageLayout } from '@jsb188/react-web/modules/layout/MainLayout';
5
6
  import { BigLoading } from '@jsb188/react-web/ui/Loading';
6
- import { useIsLoggedIn } from '@jsb188/react/states';
7
7
 
8
8
  /**
9
9
  * Auth gate; loading
10
10
  */
11
11
 
12
12
  function AuthGateLoading({ message }: { message?: string }) {
13
- return <>
14
- <BigLoading color='active' />
15
- {message && (
16
- <p className='mt_df cl_md'>
17
- {message}
18
- </p>
19
- )}
20
- </>
13
+ return (
14
+ <>
15
+ <BigLoading color='active' />
16
+ {message && <p className='mt_df cl_md'>{message}</p>}
17
+ </>
18
+ );
21
19
  }
22
20
 
23
21
  /**
@@ -25,91 +23,101 @@ function AuthGateLoading({ message }: { message?: string }) {
25
23
  */
26
24
 
27
25
  interface AuthGateProps extends ErrorMessageProps {
28
- children: React.ReactNode;
29
- notLayout?: boolean;
30
- connectedToServer?: boolean;
31
- notReady?: boolean;
32
- appLoading?: boolean; // Only used if `notLayout=true`
33
- loading?: boolean;
34
- loadingMessage?: string;
35
- defaultErrorMessage?: string;
36
- error?: ServerErrorObj | SimpleErrorType | null;
26
+ children: React.ReactNode;
27
+ notLayout?: boolean;
28
+ connectedToServer?: boolean;
29
+ notReady?: boolean;
30
+ appLoading?: boolean; // Only used if `notLayout=true`
31
+ loading?: boolean;
32
+ loadingMessage?: string;
33
+ defaultErrorMessage?: string;
34
+ error?: ServerErrorObj | SimpleErrorType | null;
37
35
  }
38
36
 
39
37
  export function AuthGate(p: AuthGateProps) {
40
- const { notReady, appLoading, loading, loadingMessage, defaultErrorMessage, error, children, notLayout, connectedToServer, ...other } = p;
41
- const isLoggedIn = useIsLoggedIn();
42
- const isNetworkError = (error as ServerErrorObj)?.errorCode === 'network_error';
43
- const isAuthLoading = loading && !isLoggedIn;
44
- const shouldRender = isLoggedIn && !notReady && !error;
45
-
46
- if (
47
- shouldRender ||
48
- ((isAuthLoading || notReady) && notLayout)
49
- ) {
50
- if (notLayout) {
51
- // The loading element needs to be absolute for `notLayout=true`
52
- // NOTE: Obviously if `appLoading=true`, then this will hide the actual content forever.
53
- // This was done intentionally-- if the route is using this feature, the route has to turn off the loading state manually from inside the route.
54
- return <>
55
- {appLoading &&
56
- <div className='abs_full v_center bg z6'>
57
- <AuthGateLoading message={loadingMessage} />
58
- </div>}
59
- {shouldRender ? children : null}
60
- </>;
61
- }
38
+ const {
39
+ notReady,
40
+ appLoading,
41
+ loading,
42
+ loadingMessage,
43
+ defaultErrorMessage,
44
+ error,
45
+ children,
46
+ notLayout,
47
+ connectedToServer,
48
+ ...other
49
+ } = p;
50
+ const isLoggedIn = useIsLoggedIn();
51
+ const isNetworkError = (error as ServerErrorObj)?.errorCode === 'network_error';
52
+ const isAuthLoading = loading && !isLoggedIn;
53
+ const shouldRender = isLoggedIn && !notReady && !error;
62
54
 
63
- // ONLY return this if `notLayout=false`
64
- return children;
65
- }
55
+ if (shouldRender || ((isAuthLoading || notReady) && notLayout)) {
56
+ if (notLayout) {
57
+ // The loading element needs to be absolute for `notLayout=true`
58
+ // NOTE: Obviously if `appLoading=true`, then this will hide the actual content forever.
59
+ // This was done intentionally-- if the route is using this feature, the route has to turn off the loading state manually from inside the route.
60
+ return (
61
+ <>
62
+ {appLoading && (
63
+ <div className='abs_full v_center bg z6'>
64
+ <AuthGateLoading message={loadingMessage} />
65
+ </div>
66
+ )}
67
+ {shouldRender ? children : null}
68
+ </>
69
+ );
70
+ }
66
71
 
67
- if (notReady || isAuthLoading) {
68
- // If `notLayout=true` this code block should NEVER happen
69
- // There should be no need to do `noLayout` check here because it should be caught above
70
- return <FullPageLayout>
71
- <AuthGateLoading message={loadingMessage} />
72
- </FullPageLayout>;
73
- }
72
+ // ONLY return this if `notLayout=false`
73
+ return children;
74
+ }
74
75
 
75
- if (!isLoggedIn && !isNetworkError) {
76
- // Gota do log in form here
77
- // Gota do log in form here
78
- // Gota do log in form here
79
- // Gota do log in form here
80
- }
76
+ if (notReady || isAuthLoading) {
77
+ // If `notLayout=true` this code block should NEVER happen
78
+ // There should be no need to do `noLayout` check here because it should be caught above
79
+ return (
80
+ <FullPageLayout>
81
+ <AuthGateLoading message={loadingMessage} />
82
+ </FullPageLayout>
83
+ );
84
+ }
81
85
 
82
- // IMPORTANT NOTE:
83
- // {connectedToServer} is currently always true, passed down from graphql hooks.
84
- // This is a placeholder logic, and must properly return a real network state in the future.
86
+ // IMPORTANT NOTE:
87
+ // {connectedToServer} is currently always true, passed down from graphql hooks.
88
+ // This is a placeholder logic, and must properly return a real network state in the future.
85
89
 
86
- let errorCode, errorMessage;
87
- if (error?.message) {
88
- errorMessage = error.message;
89
- } else if (isNetworkError || (!connectedToServer && isLoggedIn)) {
90
- errorCode = 'network_error';
91
- errorMessage = i18n.t('error.network_error');
92
- } else if (!isLoggedIn) {
93
- errorCode = '20019';
94
- errorMessage = i18n.t('error.20019');
95
- } else {
96
- errorMessage = defaultErrorMessage;
97
- }
90
+ let errorCode, errorMessage;
91
+ if (error?.message) {
92
+ errorMessage = error.message;
93
+ } else if (isNetworkError || (!connectedToServer && isLoggedIn)) {
94
+ errorCode = 'network_error';
95
+ errorMessage = i18n.t('error.network_error');
96
+ } else if (!isLoggedIn) {
97
+ errorCode = '20019';
98
+ errorMessage = i18n.t('error.20019');
99
+ } else {
100
+ errorMessage = defaultErrorMessage;
101
+ }
98
102
 
99
- const errorProps = {
100
- ...error,
101
- ...other,
102
- errorCode,
103
- message: errorMessage
104
- };
103
+ const errorProps = {
104
+ ...error,
105
+ ...other,
106
+ errorCode,
107
+ message: errorMessage,
108
+ };
105
109
 
106
- if (notLayout) {
107
- return <AppContentArea className='v_center pb_xl'>
108
- <ErrorMessage {...errorProps} />
109
- </AppContentArea>;
110
- }
110
+ if (notLayout) {
111
+ return (
112
+ <AppContentArea className='v_center pb_xl'>
113
+ <ErrorMessage {...errorProps} />
114
+ </AppContentArea>
115
+ );
116
+ }
111
117
 
112
- return <FullPageLayout>
113
- <ErrorMessage {...errorProps} />
114
- </FullPageLayout>;
118
+ return (
119
+ <FullPageLayout>
120
+ <ErrorMessage {...errorProps} />
121
+ </FullPageLayout>
122
+ );
115
123
  }
@@ -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
+ };