@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.
- package/package.json +25 -25
- package/src/hooks/use-oauth.tsx +341 -311
- package/src/modules/AssignRequiredAuth.tsx +182 -170
- package/src/modules/AuthFormUI.tsx +28 -28
- package/src/modules/AuthForms-SSO.tsx +188 -151
- package/src/modules/AuthForms.tsx +676 -607
- package/src/modules/AuthGate.tsx +108 -85
- package/src/modules/InlineAuthForm.tsx +129 -116
- package/src/modules/ManagedSessions.tsx +109 -83
- package/src/modules/OtherAuthButtons.tsx +73 -71
- package/src/modules/_helpers.ts +102 -84
- package/src/modules/schemas.ts +231 -202
- package/src/schemas/account.ts +153 -138
- package/src/schemas/address.ts +155 -138
- package/src/ui/ManagedSessionUI.tsx +70 -64
package/src/modules/AuthGate.tsx
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
-
import type {
|
|
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 {
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
// ONLY return this if `notLayout=false`
|
|
81
|
+
return children;
|
|
82
|
+
}
|
|
66
83
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
118
|
+
const errorProps = {
|
|
119
|
+
...error,
|
|
120
|
+
...other,
|
|
121
|
+
errorCode,
|
|
122
|
+
message: errorMessage,
|
|
123
|
+
};
|
|
105
124
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
125
|
+
if (notLayout) {
|
|
126
|
+
return (
|
|
127
|
+
<AppContentArea className="v_center pb_xl">
|
|
128
|
+
<ErrorMessage {...errorProps} />
|
|
129
|
+
</AppContentArea>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
111
132
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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 {
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
const {
|
|
52
|
+
googleLabel,
|
|
53
|
+
microsoftLabel,
|
|
54
|
+
respondentEmail,
|
|
55
|
+
accountMessage,
|
|
56
|
+
authenticateOnly,
|
|
57
|
+
formPublicId,
|
|
58
|
+
onSignedIn,
|
|
59
|
+
onRespondentToken,
|
|
60
|
+
} = p;
|
|
58
61
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
const onGoogle = useCallback(() => {
|
|
94
|
+
onInitiateGoogleSignIn(
|
|
95
|
+
undefined,
|
|
96
|
+
null,
|
|
97
|
+
formPublicId ?? null,
|
|
98
|
+
authenticateOnly,
|
|
99
|
+
);
|
|
100
|
+
}, [onInitiateGoogleSignIn, formPublicId, authenticateOnly]);
|
|
92
101
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
102
|
+
const onMicrosoft = useCallback(() => {
|
|
103
|
+
onInitiateMicrosoftSignIn(
|
|
104
|
+
undefined,
|
|
105
|
+
undefined,
|
|
106
|
+
formPublicId ?? null,
|
|
107
|
+
authenticateOnly,
|
|
108
|
+
);
|
|
109
|
+
}, [onInitiateMicrosoftSignIn, formPublicId, authenticateOnly]);
|
|
96
110
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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);
|