@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/package.json +27 -25
- package/src/hooks/use-auth-popup.ts +167 -0
- package/src/hooks/use-google-one-tap.ts +138 -0
- package/src/hooks/use-oauth.tsx +314 -310
- package/src/main.ts +11 -0
- package/src/modules/AssignRequiredAuth.tsx +217 -171
- package/src/modules/AuthFormUI.tsx +28 -28
- package/src/modules/AuthForms-SSO.tsx +174 -152
- package/src/modules/AuthForms.tsx +814 -612
- package/src/modules/AuthGate.tsx +93 -85
- package/src/modules/ConfirmPublicFormEmailCode.tsx +144 -0
- package/src/modules/InlineAuthForm.tsx +441 -130
- package/src/modules/ManagedSessions.tsx +86 -80
- package/src/modules/OtherAuthButtons.tsx +72 -68
- package/src/modules/_helpers.ts +161 -86
- package/src/modules/schemas.ts +159 -260
- package/src/schemas/account.ts +106 -154
- package/src/schemas/address.ts +125 -157
- package/src/ui/ManagedSessionUI.tsx +70 -64
- package/src/utils/authPopup.ts +95 -0
- package/src/utils/googleIdentity.ts +100 -0
package/src/modules/AuthGate.tsx
CHANGED
|
@@ -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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
64
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return <FullPageLayout>
|
|
71
|
-
<AuthGateLoading message={loadingMessage} />
|
|
72
|
-
</FullPageLayout>;
|
|
73
|
-
}
|
|
72
|
+
// ONLY return this if `notLayout=false`
|
|
73
|
+
return children;
|
|
74
|
+
}
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
const errorProps = {
|
|
104
|
+
...error,
|
|
105
|
+
...other,
|
|
106
|
+
errorCode,
|
|
107
|
+
message: errorMessage,
|
|
108
|
+
};
|
|
105
109
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
110
|
+
if (notLayout) {
|
|
111
|
+
return (
|
|
112
|
+
<AppContentArea className='v_center pb_xl'>
|
|
113
|
+
<ErrorMessage {...errorProps} />
|
|
114
|
+
</AppContentArea>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
111
117
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
+
};
|