@jsb188/auth-web 1.1.10 → 1.1.11

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,7 +1,14 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
- import type { ServerErrorObj, SimpleErrorType } from '@jsb188/app/types/app.d.ts';
2
+ import type {
3
+ ServerErrorObj,
4
+ SimpleErrorType,
5
+ } from '@jsb188/app/types/app.d.ts';
3
6
  import type { ErrorMessageProps } from '@jsb188/react-web/modules/layout/MainLayout';
4
- import { AppContentArea, ErrorMessage, FullPageLayout } from '@jsb188/react-web/modules/layout/MainLayout';
7
+ import {
8
+ AppContentArea,
9
+ ErrorMessage,
10
+ FullPageLayout,
11
+ } from '@jsb188/react-web/modules/layout/MainLayout';
5
12
  import { BigLoading } from '@jsb188/react-web/ui/Loading';
6
13
  import { useIsLoggedIn } from '@jsb188/react/states';
7
14
 
@@ -10,14 +17,12 @@ import { useIsLoggedIn } from '@jsb188/react/states';
10
17
  */
11
18
 
12
19
  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
- </>
20
+ return (
21
+ <>
22
+ <BigLoading color="active" />
23
+ {message && <p className="mt_df cl_md">{message}</p>}
24
+ </>
25
+ );
21
26
  }
22
27
 
23
28
  /**
@@ -25,91 +30,109 @@ function AuthGateLoading({ message }: { message?: string }) {
25
30
  */
26
31
 
27
32
  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;
33
+ children: React.ReactNode;
34
+ notLayout?: boolean;
35
+ connectedToServer?: boolean;
36
+ notReady?: boolean;
37
+ appLoading?: boolean; // Only used if `notLayout=true`
38
+ loading?: boolean;
39
+ loadingMessage?: string;
40
+ defaultErrorMessage?: string;
41
+ error?: ServerErrorObj | SimpleErrorType | null;
37
42
  }
38
43
 
39
44
  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
+ const {
46
+ notReady,
47
+ appLoading,
48
+ loading,
49
+ loadingMessage,
50
+ defaultErrorMessage,
51
+ error,
52
+ children,
53
+ notLayout,
54
+ connectedToServer,
55
+ ...other
56
+ } = p;
57
+ const isLoggedIn = useIsLoggedIn();
58
+ const isNetworkError =
59
+ (error as ServerErrorObj)?.errorCode === 'network_error';
60
+ const isAuthLoading = loading && !isLoggedIn;
61
+ const shouldRender = isLoggedIn && !notReady && !error;
45
62
 
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
- }
63
+ if (shouldRender || ((isAuthLoading || notReady) && notLayout)) {
64
+ if (notLayout) {
65
+ // The loading element needs to be absolute for `notLayout=true`
66
+ // NOTE: Obviously if `appLoading=true`, then this will hide the actual content forever.
67
+ // 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.
68
+ return (
69
+ <>
70
+ {appLoading && (
71
+ <div className="abs_full v_center bg z6">
72
+ <AuthGateLoading message={loadingMessage} />
73
+ </div>
74
+ )}
75
+ {shouldRender ? children : null}
76
+ </>
77
+ );
78
+ }
62
79
 
63
- // ONLY return this if `notLayout=false`
64
- return children;
65
- }
80
+ // ONLY return this if `notLayout=false`
81
+ return children;
82
+ }
66
83
 
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
- }
84
+ if (notReady || isAuthLoading) {
85
+ // If `notLayout=true` this code block should NEVER happen
86
+ // There should be no need to do `noLayout` check here because it should be caught above
87
+ return (
88
+ <FullPageLayout>
89
+ <AuthGateLoading message={loadingMessage} />
90
+ </FullPageLayout>
91
+ );
92
+ }
74
93
 
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
- }
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
+ }
81
100
 
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.
101
+ // IMPORTANT NOTE:
102
+ // {connectedToServer} is currently always true, passed down from graphql hooks.
103
+ // This is a placeholder logic, and must properly return a real network state in the future.
85
104
 
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
- }
105
+ let errorCode, errorMessage;
106
+ if (error?.message) {
107
+ errorMessage = error.message;
108
+ } else if (isNetworkError || (!connectedToServer && isLoggedIn)) {
109
+ errorCode = 'network_error';
110
+ errorMessage = i18n.t('error.network_error');
111
+ } else if (!isLoggedIn) {
112
+ errorCode = '20019';
113
+ errorMessage = i18n.t('error.20019');
114
+ } else {
115
+ errorMessage = defaultErrorMessage;
116
+ }
98
117
 
99
- const errorProps = {
100
- ...error,
101
- ...other,
102
- errorCode,
103
- message: errorMessage
104
- };
118
+ const errorProps = {
119
+ ...error,
120
+ ...other,
121
+ errorCode,
122
+ message: errorMessage,
123
+ };
105
124
 
106
- if (notLayout) {
107
- return <AppContentArea className='v_center pb_xl'>
108
- <ErrorMessage {...errorProps} />
109
- </AppContentArea>;
110
- }
125
+ if (notLayout) {
126
+ return (
127
+ <AppContentArea className="v_center pb_xl">
128
+ <ErrorMessage {...errorProps} />
129
+ </AppContentArea>
130
+ );
131
+ }
111
132
 
112
- return <FullPageLayout>
113
- <ErrorMessage {...errorProps} />
114
- </FullPageLayout>;
133
+ return (
134
+ <FullPageLayout>
135
+ <ErrorMessage {...errorProps} />
136
+ </FullPageLayout>
137
+ );
115
138
  }
@@ -4,36 +4,39 @@ import { AvatarImg } from '@jsb188/react-web/ui/Avatar';
4
4
  import { PageFormHeaderOAuthButton } from '@jsb188/react-web/ui/PageFormUI';
5
5
  import { TextWithLinks } from '@jsb188/react-web/ui/Markdown';
6
6
  import { memo, useCallback, type ReactNode } from 'react';
7
- import { useContinueWithGoogle, useContinueWithMicrosoft } from '../hooks/use-oauth';
7
+ import {
8
+ useContinueWithGoogle,
9
+ useContinueWithMicrosoft,
10
+ } from '../hooks/use-oauth';
8
11
 
9
12
  /**
10
13
  * Types
11
14
  */
12
15
 
13
16
  interface InlineAuthFormProps {
14
- // Sign-in button labels; a null/absent label hides that provider's button
15
- googleLabel?: string | null;
16
- microsoftLabel?: string | null;
17
- // Verified email of a temporary (account-less) respondent token holder; shown
18
- // as the identity when there is no signed-in account
19
- respondentEmail?: string | null;
20
- // Respondent status / helper text shown next to the buttons; a plain string
21
- // is parsed for inline links, a node is rendered as-is
22
- accountMessage?: ReactNode;
23
- // When true, an OAuth-verified email with no account does not error; the
24
- // server returns a temporary respondent token (ANYONE web forms)
25
- authenticateOnly?: boolean;
26
- // Public web form context (contact-gated account creation / respondent token)
27
- formPublicId?: string | null;
28
- // Called after a real account sign-in flips the app into a logged-in state,
29
- // with the fresh auth token (e.g. to silently persist the SSR session cookie)
30
- onSignedIn?: (
31
- token: string,
32
- sessionBootstrapToken?: string | null,
33
- sessionPersisted?: boolean,
34
- ) => boolean | Promise<boolean>;
35
- // Called when authenticateOnly returned a temporary form respondent token
36
- onRespondentToken?: (token: string, email: string | null) => void;
17
+ // Sign-in button labels; a null/absent label hides that provider's button
18
+ googleLabel?: string | null;
19
+ microsoftLabel?: string | null;
20
+ // Verified email of a temporary (account-less) respondent token holder; shown
21
+ // as the identity when there is no signed-in account
22
+ respondentEmail?: string | null;
23
+ // Respondent status / helper text shown next to the buttons; a plain string
24
+ // is parsed for inline links, a node is rendered as-is
25
+ accountMessage?: ReactNode;
26
+ // When true, an OAuth-verified email with no account does not error; the
27
+ // server returns a temporary respondent token (ANYONE web forms)
28
+ authenticateOnly?: boolean;
29
+ // Public web form context (contact-gated account creation / respondent token)
30
+ formPublicId?: string | null;
31
+ // Called after a real account sign-in flips the app into a logged-in state,
32
+ // with the fresh auth token (e.g. to silently persist the SSR session cookie)
33
+ onSignedIn?: (
34
+ token: string,
35
+ sessionBootstrapToken?: string | null,
36
+ sessionPersisted?: boolean,
37
+ ) => boolean | Promise<boolean>;
38
+ // Called when authenticateOnly returned a temporary form respondent token
39
+ onRespondentToken?: (token: string, email: string | null) => void;
37
40
  }
38
41
 
39
42
  /*
@@ -45,104 +48,114 @@ interface InlineAuthFormProps {
45
48
  */
46
49
 
47
50
  const InlineAuthForm = (p: InlineAuthFormProps) => {
48
- const {
49
- googleLabel,
50
- microsoftLabel,
51
- respondentEmail,
52
- accountMessage,
53
- authenticateOnly,
54
- formPublicId,
55
- onSignedIn,
56
- onRespondentToken,
57
- } = p;
51
+ const {
52
+ googleLabel,
53
+ microsoftLabel,
54
+ respondentEmail,
55
+ accountMessage,
56
+ authenticateOnly,
57
+ formPublicId,
58
+ onSignedIn,
59
+ onRespondentToken,
60
+ } = p;
58
61
 
59
- // A signed-in account takes precedence; otherwise a temporary respondent
60
- // token holder is identified by their verified email
61
- const { isLoggedIn, displayName, labelName } = useCurrentAccount();
62
- const signedInName = isLoggedIn
63
- ? (displayName || labelName)
64
- : (respondentEmail || null);
62
+ // A signed-in account takes precedence; otherwise a temporary respondent
63
+ // token holder is identified by their verified email
64
+ const { isLoggedIn, displayName, labelName } = useCurrentAccount();
65
+ const signedInName = isLoggedIn
66
+ ? displayName || labelName
67
+ : respondentEmail || null;
65
68
 
66
- /*
67
- * A real account sign-in already applied the logged-in app state; hand the
68
- * token to the caller for any follow-up (e.g. persisting the session cookie).
69
- */
70
- const onFinishSignIn = useCallback(
71
- (token: string, sessionBootstrapToken?: string | null, sessionPersisted?: boolean) =>
72
- onSignedIn?.(token, sessionBootstrapToken, sessionPersisted) ?? false,
73
- [onSignedIn],
74
- );
69
+ /*
70
+ * A real account sign-in already applied the logged-in app state; hand the
71
+ * token to the caller for any follow-up (e.g. persisting the session cookie).
72
+ */
73
+ const onFinishSignIn = useCallback(
74
+ (
75
+ token: string,
76
+ sessionBootstrapToken?: string | null,
77
+ sessionPersisted?: boolean,
78
+ ) => onSignedIn?.(token, sessionBootstrapToken, sessionPersisted) ?? false,
79
+ [onSignedIn],
80
+ );
75
81
 
76
- const {
77
- onInitiateGoogleSignIn,
78
- oauthButtonDisabled: googleDisabled,
79
- waitingForOAuth: googleLoading,
80
- } =
81
- useContinueWithGoogle({ onFinishSignIn, onRespondentToken });
82
- const {
83
- onInitiateMicrosoftSignIn,
84
- oauthButtonDisabled: microsoftDisabled,
85
- waitingForOAuth: microsoftLoading,
86
- } =
87
- useContinueWithMicrosoft({ onFinishSignIn, onRespondentToken });
82
+ const {
83
+ onInitiateGoogleSignIn,
84
+ oauthButtonDisabled: googleDisabled,
85
+ waitingForOAuth: googleLoading,
86
+ } = useContinueWithGoogle({ onFinishSignIn, onRespondentToken });
87
+ const {
88
+ onInitiateMicrosoftSignIn,
89
+ oauthButtonDisabled: microsoftDisabled,
90
+ waitingForOAuth: microsoftLoading,
91
+ } = useContinueWithMicrosoft({ onFinishSignIn, onRespondentToken });
88
92
 
89
- const onGoogle = useCallback(() => {
90
- onInitiateGoogleSignIn(undefined, null, formPublicId ?? null, authenticateOnly);
91
- }, [onInitiateGoogleSignIn, formPublicId, authenticateOnly]);
93
+ const onGoogle = useCallback(() => {
94
+ onInitiateGoogleSignIn(
95
+ undefined,
96
+ null,
97
+ formPublicId ?? null,
98
+ authenticateOnly,
99
+ );
100
+ }, [onInitiateGoogleSignIn, formPublicId, authenticateOnly]);
92
101
 
93
- const onMicrosoft = useCallback(() => {
94
- onInitiateMicrosoftSignIn(undefined, undefined, formPublicId ?? null, authenticateOnly);
95
- }, [onInitiateMicrosoftSignIn, formPublicId, authenticateOnly]);
102
+ const onMicrosoft = useCallback(() => {
103
+ onInitiateMicrosoftSignIn(
104
+ undefined,
105
+ undefined,
106
+ formPublicId ?? null,
107
+ authenticateOnly,
108
+ );
109
+ }, [onInitiateMicrosoftSignIn, formPublicId, authenticateOnly]);
96
110
 
97
- return (
98
- <div className='h_item gap_sm min_w_0 f_wrap rel'>
99
- {signedInName ? (
100
- <div className='h_item gap_8 min_w_0'>
101
- <AvatarImg
102
- className='shift_up'
103
- displayName={signedInName}
104
- size='small'
105
- letterBackgroundClassName='bd_2 bd_lt bg'
106
- />
107
- <p className='ft_medium'>
108
- {i18n.t('auth.logged_in_as_', { name: signedInName })}
109
- </p>
110
- </div>
111
- ) : (
112
- (!!googleLabel || !!microsoftLabel) && (
113
- <div className='h_item gap_5 no_shrink'>
114
- {!!googleLabel && (
115
- <PageFormHeaderOAuthButton
116
- iconName='brand-google-color'
117
- label={googleLabel}
118
- disabled={googleDisabled}
119
- loading={googleLoading}
120
- onClick={onGoogle}
121
- />
122
- )}
123
- {!!microsoftLabel && (
124
- <PageFormHeaderOAuthButton
125
- iconName='brand-microsoft-color'
126
- label={microsoftLabel}
127
- disabled={microsoftDisabled}
128
- loading={microsoftLoading}
129
- onClick={onMicrosoft}
130
- />
131
- )}
132
- </div>
133
- )
134
- )}
135
- {!!accountMessage && (
136
- typeof accountMessage === 'string'
137
- ? (
138
- <TextWithLinks as='p' className='cl_md ml_xs' linkClassName='u cl_df'>
139
- {accountMessage}
140
- </TextWithLinks>
141
- )
142
- : <p className='cl_md ft_md'>{accountMessage}</p>
143
- )}
144
- </div>
145
- );
111
+ return (
112
+ <div className="h_item gap_sm min_w_0 f_wrap rel">
113
+ {signedInName ? (
114
+ <div className="h_item gap_8 min_w_0">
115
+ <AvatarImg
116
+ className="shift_up"
117
+ displayName={signedInName}
118
+ size="small"
119
+ letterBackgroundClassName="bd_2 bd_lt bg"
120
+ />
121
+ <p className="ft_medium">
122
+ {i18n.t('auth.logged_in_as_', { name: signedInName })}
123
+ </p>
124
+ </div>
125
+ ) : (
126
+ (!!googleLabel || !!microsoftLabel) && (
127
+ <div className="h_item gap_5 no_shrink">
128
+ {!!googleLabel && (
129
+ <PageFormHeaderOAuthButton
130
+ iconName="brand-google-color"
131
+ label={googleLabel}
132
+ disabled={googleDisabled}
133
+ loading={googleLoading}
134
+ onClick={onGoogle}
135
+ />
136
+ )}
137
+ {!!microsoftLabel && (
138
+ <PageFormHeaderOAuthButton
139
+ iconName="brand-microsoft-color"
140
+ label={microsoftLabel}
141
+ disabled={microsoftDisabled}
142
+ loading={microsoftLoading}
143
+ onClick={onMicrosoft}
144
+ />
145
+ )}
146
+ </div>
147
+ )
148
+ )}
149
+ {!!accountMessage &&
150
+ (typeof accountMessage === 'string' ? (
151
+ <TextWithLinks as="p" className="cl_md ml_xs" linkClassName="u cl_df">
152
+ {accountMessage}
153
+ </TextWithLinks>
154
+ ) : (
155
+ <p className="cl_md ft_md">{accountMessage}</p>
156
+ ))}
157
+ </div>
158
+ );
146
159
  };
147
160
 
148
161
  export default memo(InlineAuthForm);