@jsb188/auth-web 1.1.3 → 1.1.10

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.
@@ -0,0 +1,45 @@
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import { cn } from '@jsb188/app/utils/string.ts';
3
+ import { Icon } from '@jsb188/react-web/svgs/Icon';
4
+ import { memo } from 'react';
5
+
6
+ /**
7
+ * Auth form container
8
+ */
9
+
10
+ interface AuthFormContainerProps {
11
+ title?: string;
12
+ className?: string;
13
+ backHref?: string | null;
14
+ children: React.ReactNode;
15
+ }
16
+
17
+ export const AuthFormContainer = memo((p: AuthFormContainerProps) => {
18
+ const { title, className, backHref, children } = p;
19
+ const goBackLabel = backHref ? i18n.t('form.go_back') : '';
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
+ );
43
+ });
44
+
45
+ AuthFormContainer.displayName = 'AuthFormContainer';
@@ -0,0 +1,187 @@
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import { useGetSsoLoginOptions } from '@jsb188/graphql/hooks/use-auth-mtn';
3
+ import SchemaForm from '@jsb188/react-web/modules/SchemaForm';
4
+ import { FullWidthButton } from '@jsb188/react-web/ui/Button';
5
+ import { ModalErrorMessage } from '@jsb188/react-web/ui/ModalUI';
6
+ import { useCallback, useMemo, useState } from 'react';
7
+ import { useNavigate } from 'react-router';
8
+ import type { OnFinishSignInFn, SsoLoginOptionData } from '../hooks/use-oauth';
9
+ import { useContinueWithGoogle, useContinueWithMicrosoft } from '../hooks/use-oauth';
10
+ import { getAuthRoute, persistSignInSession } from './_helpers';
11
+ import { makeRequestTokenizedEmailSchema } from './schemas';
12
+
13
+ /**
14
+ * Types
15
+ */
16
+
17
+ interface SsoSignInProps {
18
+ authRoutesMap?: Record<string, string>;
19
+ setAuthIface: (iface: null) => void;
20
+ }
21
+
22
+ /**
23
+ * SSO org picker; one button per matching org SSO connection, clicking it
24
+ * launches that org's provider flow (the click is the user gesture that is
25
+ * allowed to open the OAuth popup)
26
+ */
27
+
28
+ function SsoOrgOptions(p: {
29
+ email: string;
30
+ options: SsoLoginOptionData[];
31
+ onFinishSignIn: OnFinishSignInFn;
32
+ }) {
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
+ </>;
88
+ }
89
+
90
+ /**
91
+ * SSO sign in (email-first home realm discovery); asks for the email
92
+ * address, resolves which org SSO connections can sign it in, then routes
93
+ * to the matching provider flow
94
+ */
95
+
96
+ 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
+ </>;
187
+ }