@proteinjs/user-ui 1.5.1 → 1.6.1
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/CHANGELOG.md +25 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/src/auth/AuthApi.d.ts +19 -2
- package/dist/src/auth/AuthApi.d.ts.map +1 -1
- package/dist/src/auth/AuthApi.js +51 -3
- package/dist/src/auth/AuthApi.js.map +1 -1
- package/dist/src/auth/AuthTextField.d.ts +9 -2
- package/dist/src/auth/AuthTextField.d.ts.map +1 -1
- package/dist/src/auth/AuthTextField.js +7 -4
- package/dist/src/auth/AuthTextField.js.map +1 -1
- package/dist/src/pages/ForgotPassword.d.ts.map +1 -1
- package/dist/src/pages/ForgotPassword.js +1 -1
- package/dist/src/pages/ForgotPassword.js.map +1 -1
- package/dist/src/pages/Login.d.ts.map +1 -1
- package/dist/src/pages/Login.js +1 -1
- package/dist/src/pages/Login.js.map +1 -1
- package/dist/src/pages/PasswordReset.d.ts.map +1 -1
- package/dist/src/pages/PasswordReset.js +15 -7
- package/dist/src/pages/PasswordReset.js.map +1 -1
- package/dist/src/pages/Signup.d.ts.map +1 -1
- package/dist/src/pages/Signup.js +37 -20
- package/dist/src/pages/Signup.js.map +1 -1
- package/dist/test/authApi.test.js +26 -5
- package/dist/test/authApi.test.js.map +1 -1
- package/dist/test/authPages.test.js +7 -2
- package/dist/test/authPages.test.js.map +1 -1
- package/dist/test/passwordResetAutofill.test.d.ts +5 -0
- package/dist/test/passwordResetAutofill.test.d.ts.map +1 -0
- package/dist/test/passwordResetAutofill.test.js +168 -0
- package/dist/test/passwordResetAutofill.test.js.map +1 -0
- package/dist/test/signupAutofill.test.d.ts +5 -0
- package/dist/test/signupAutofill.test.d.ts.map +1 -0
- package/dist/test/signupAutofill.test.js +196 -0
- package/dist/test/signupAutofill.test.js.map +1 -0
- package/generated/index.ts +1 -1
- package/package.json +8 -7
- package/src/auth/AuthApi.ts +33 -4
- package/src/auth/AuthTextField.tsx +16 -6
- package/src/pages/ForgotPassword.tsx +3 -1
- package/src/pages/Login.tsx +3 -1
- package/src/pages/PasswordReset.tsx +12 -2
- package/src/pages/Signup.tsx +25 -10
- package/test/authApi.test.ts +19 -5
- package/test/authPages.test.tsx +7 -2
- package/test/passwordResetAutofill.test.tsx +92 -0
- package/test/signupAutofill.test.tsx +115 -0
package/src/auth/AuthApi.ts
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
1
|
-
import { routes } from '@proteinjs/user';
|
|
1
|
+
import { UserSignup, routes } from '@proteinjs/user';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* The auth flows' server calls, in one place so the pages hold presentation only.
|
|
5
5
|
* Methods resolve on success and throw an `Error` whose message is user-readable.
|
|
6
6
|
*/
|
|
7
7
|
export class AuthApi {
|
|
8
|
+
/**
|
|
9
|
+
* Signs up AND establishes the session in the same request (auto-login): on resolve the
|
|
10
|
+
* caller navigates straight into the app — no bounce through the login form. Invited users
|
|
11
|
+
* pass the invite `token` and no email (the invite carries it).
|
|
12
|
+
*/
|
|
13
|
+
async signup(user: UserSignup, inviteToken?: string): Promise<void> {
|
|
14
|
+
const response = await fetch(routes.signup.path, {
|
|
15
|
+
method: routes.signup.method,
|
|
16
|
+
body: JSON.stringify({ ...user, token: inviteToken }),
|
|
17
|
+
redirect: 'follow',
|
|
18
|
+
credentials: 'same-origin',
|
|
19
|
+
headers: { 'Content-Type': 'application/json' },
|
|
20
|
+
});
|
|
21
|
+
if (response.status != 200) {
|
|
22
|
+
throw new Error(`Failed to sign up, error: ${response.statusText}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const body = await response.json();
|
|
26
|
+
if (body.error) {
|
|
27
|
+
throw new Error(body.error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
8
31
|
async login(email: string, password: string): Promise<void> {
|
|
9
32
|
const response = await fetch(routes.login.path, {
|
|
10
33
|
method: routes.login.method,
|
|
@@ -41,16 +64,22 @@ export class AuthApi {
|
|
|
41
64
|
}
|
|
42
65
|
}
|
|
43
66
|
|
|
44
|
-
/**
|
|
45
|
-
|
|
67
|
+
/**
|
|
68
|
+
* A valid token resolves with the account email the reset is for — the page renders it as
|
|
69
|
+
* the read-only `autocomplete="username"` field so password managers can associate the
|
|
70
|
+
* updated password with the stored credential.
|
|
71
|
+
*/
|
|
72
|
+
async validateResetToken(token: string): Promise<{ valid: true; email: string } | { valid: false; message: string }> {
|
|
46
73
|
const response = await fetch(`${routes.validateResetToken.path}?token=${token}`, {
|
|
47
74
|
method: routes.validateResetToken.method,
|
|
48
75
|
credentials: 'same-origin',
|
|
49
76
|
});
|
|
50
77
|
const body = await response.json();
|
|
51
78
|
if (!body.isValid) {
|
|
52
|
-
return body.message || 'Invalid or expired token';
|
|
79
|
+
return { valid: false, message: body.message || 'Invalid or expired token' };
|
|
53
80
|
}
|
|
81
|
+
|
|
82
|
+
return { valid: true, email: body.email };
|
|
54
83
|
}
|
|
55
84
|
|
|
56
85
|
async executePasswordReset(token: string, newPassword: string): Promise<void> {
|
|
@@ -6,10 +6,17 @@ import VisibilityOff from '@mui/icons-material/VisibilityOff';
|
|
|
6
6
|
export interface AuthTextFieldProps {
|
|
7
7
|
label: string;
|
|
8
8
|
value: string;
|
|
9
|
-
|
|
9
|
+
/** Omitted for read-only fields. */
|
|
10
|
+
onChange?: (value: string) => void;
|
|
10
11
|
/** Renders a reveal toggle and masks input until toggled. */
|
|
11
12
|
password?: boolean;
|
|
12
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Renders the value as fixed (e.g. the invite's email on signup). Read-only rather than
|
|
15
|
+
* disabled on purpose: password managers ignore disabled inputs but do read read-only ones —
|
|
16
|
+
* which matters when the field is the credential's username.
|
|
17
|
+
*/
|
|
18
|
+
readOnly?: boolean;
|
|
19
|
+
/** HTML autocomplete token (e.g. 'username', 'current-password', 'new-password'). */
|
|
13
20
|
autoComplete?: string;
|
|
14
21
|
/** HTML input type when not a password field (e.g. 'email'). */
|
|
15
22
|
type?: string;
|
|
@@ -23,7 +30,7 @@ export interface AuthTextFieldProps {
|
|
|
23
30
|
* target; 1rem input text so mobile browsers don't auto-zoom the field on focus.
|
|
24
31
|
*/
|
|
25
32
|
export function AuthTextField(props: AuthTextFieldProps) {
|
|
26
|
-
const { label, value, onChange, password, autoComplete, type, autoFocus, disabled } = props;
|
|
33
|
+
const { label, value, onChange, password, readOnly, autoComplete, type, autoFocus, disabled } = props;
|
|
27
34
|
const [revealed, setRevealed] = useState(false);
|
|
28
35
|
|
|
29
36
|
return (
|
|
@@ -39,12 +46,13 @@ export function AuthTextField(props: AuthTextFieldProps) {
|
|
|
39
46
|
id={`auth-field-${label}`}
|
|
40
47
|
fullWidth
|
|
41
48
|
value={value}
|
|
42
|
-
onChange={(event) => onChange(event.target.value)}
|
|
49
|
+
onChange={(event) => onChange?.(event.target.value)}
|
|
43
50
|
type={password && !revealed ? 'password' : password ? 'text' : type || 'text'}
|
|
44
51
|
autoComplete={autoComplete}
|
|
45
52
|
autoFocus={autoFocus}
|
|
46
53
|
disabled={disabled}
|
|
47
54
|
InputProps={{
|
|
55
|
+
readOnly,
|
|
48
56
|
endAdornment: password ? (
|
|
49
57
|
<InputAdornment position='end'>
|
|
50
58
|
<IconButton
|
|
@@ -61,11 +69,13 @@ export function AuthTextField(props: AuthTextFieldProps) {
|
|
|
61
69
|
sx={{
|
|
62
70
|
'& .MuiOutlinedInput-root': {
|
|
63
71
|
borderRadius: '12px',
|
|
64
|
-
|
|
72
|
+
// A read-only field wears a subtle fill and keeps a quiet border, so it reads as a
|
|
73
|
+
// fixed value rather than an input that ignores typing.
|
|
74
|
+
backgroundColor: readOnly ? 'action.hover' : 'background.paper',
|
|
65
75
|
fontSize: '1rem',
|
|
66
76
|
'& input': { py: '12.5px', px: '14px' },
|
|
67
77
|
'& fieldset': { borderColor: 'divider' },
|
|
68
|
-
'&:hover fieldset': { borderColor: 'text.disabled' },
|
|
78
|
+
'&:hover fieldset': { borderColor: readOnly ? 'divider' : 'text.disabled' },
|
|
69
79
|
'&.Mui-focused fieldset': { borderColor: 'primary.main', borderWidth: '1.5px' },
|
|
70
80
|
},
|
|
71
81
|
}}
|
|
@@ -50,12 +50,14 @@ const ForgotPasswordComponent: React.FC = () => {
|
|
|
50
50
|
subtitle={`Enter your email and we'll send you a link to reset your password.`}
|
|
51
51
|
>
|
|
52
52
|
<form onSubmit={onSubmit} noValidate>
|
|
53
|
+
{/* Same identifier token as login ('username'), so the password manager fills the
|
|
54
|
+
stored username here too. */}
|
|
53
55
|
<AuthTextField
|
|
54
56
|
label='Email'
|
|
55
57
|
value={email}
|
|
56
58
|
onChange={setEmail}
|
|
57
59
|
type='email'
|
|
58
|
-
autoComplete='
|
|
60
|
+
autoComplete='username'
|
|
59
61
|
disabled={busy}
|
|
60
62
|
/>
|
|
61
63
|
<AuthFormError message={error} />
|
package/src/pages/Login.tsx
CHANGED
|
@@ -35,12 +35,14 @@ const LoginComponent: React.FC = () => {
|
|
|
35
35
|
</Helmet>
|
|
36
36
|
<AuthLayout title='Log in'>
|
|
37
37
|
<form onSubmit={onSubmit} noValidate>
|
|
38
|
+
{/* 'username' (not 'email'): this is the credential identifier password managers
|
|
39
|
+
fill from the saved login; type='email' still gives the email keyboard. */}
|
|
38
40
|
<AuthTextField
|
|
39
41
|
label='Email'
|
|
40
42
|
value={email}
|
|
41
43
|
onChange={setEmail}
|
|
42
44
|
type='email'
|
|
43
|
-
autoComplete='
|
|
45
|
+
autoComplete='username'
|
|
44
46
|
disabled={busy}
|
|
45
47
|
/>
|
|
46
48
|
<AuthTextField
|
|
@@ -25,6 +25,7 @@ const PasswordResetComponent: React.FC = () => {
|
|
|
25
25
|
const [token] = useState(resetTokenFromUrl);
|
|
26
26
|
const [validating, setValidating] = useState(!!token);
|
|
27
27
|
const [invalid, setInvalid] = useState(!token);
|
|
28
|
+
const [email, setEmail] = useState('');
|
|
28
29
|
const [newPassword, setNewPassword] = useState('');
|
|
29
30
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
30
31
|
const [error, setError] = useState<string | undefined>();
|
|
@@ -39,8 +40,13 @@ const PasswordResetComponent: React.FC = () => {
|
|
|
39
40
|
let active = true;
|
|
40
41
|
new AuthApi()
|
|
41
42
|
.validateResetToken(token)
|
|
42
|
-
.then((
|
|
43
|
-
if (active
|
|
43
|
+
.then((result) => {
|
|
44
|
+
if (!active) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (result.valid) {
|
|
48
|
+
setEmail(result.email);
|
|
49
|
+
} else {
|
|
44
50
|
setInvalid(true);
|
|
45
51
|
}
|
|
46
52
|
})
|
|
@@ -118,6 +124,10 @@ const PasswordResetComponent: React.FC = () => {
|
|
|
118
124
|
</Helmet>
|
|
119
125
|
<AuthLayout title='Choose a new password'>
|
|
120
126
|
<form onSubmit={onSubmit} noValidate>
|
|
127
|
+
{/* The account the reset is for, read-only and tagged `username`: without an
|
|
128
|
+
identifier field, password managers can't associate the new password with the
|
|
129
|
+
stored credential (read-only, not disabled — managers ignore disabled inputs). */}
|
|
130
|
+
<AuthTextField label='Email' value={email} type='email' autoComplete='username' readOnly />
|
|
121
131
|
<AuthTextField
|
|
122
132
|
label='New password'
|
|
123
133
|
value={newPassword}
|
package/src/pages/Signup.tsx
CHANGED
|
@@ -3,13 +3,13 @@ import { Skeleton, Stack } from '@mui/material';
|
|
|
3
3
|
import { Page } from '@proteinjs/ui';
|
|
4
4
|
import { getSignupService, uiRoutes } from '@proteinjs/user';
|
|
5
5
|
import { Helmet } from 'react-helmet';
|
|
6
|
-
import { useNavigate } from 'react-router-dom';
|
|
7
6
|
import { AuthLayout } from '../auth/AuthLayout';
|
|
8
7
|
import { AuthTextField } from '../auth/AuthTextField';
|
|
9
8
|
import { AuthButton } from '../auth/AuthButton';
|
|
10
9
|
import { AuthFormError } from '../auth/AuthFormError';
|
|
11
10
|
import { AuthMessagePanel } from '../auth/AuthMessagePanel';
|
|
12
11
|
import { AuthValidation } from '../auth/AuthValidation';
|
|
12
|
+
import { AuthApi } from '../auth/AuthApi';
|
|
13
13
|
|
|
14
14
|
function inviteTokenFromUrl(): string {
|
|
15
15
|
if (typeof window === 'undefined') {
|
|
@@ -20,12 +20,12 @@ function inviteTokenFromUrl(): string {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
const SignupComponent: React.FC = () => {
|
|
23
|
-
const navigate = useNavigate();
|
|
24
23
|
const [token] = useState(inviteTokenFromUrl);
|
|
25
24
|
const [initializing, setInitializing] = useState(true);
|
|
26
25
|
const [initializationError, setInitializationError] = useState<string | null>(null);
|
|
27
26
|
const [name, setName] = useState('');
|
|
28
27
|
const [email, setEmail] = useState('');
|
|
28
|
+
const [inviteEmail, setInviteEmail] = useState('');
|
|
29
29
|
const [password, setPassword] = useState('');
|
|
30
30
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
31
31
|
const [error, setError] = useState<string | undefined>();
|
|
@@ -36,8 +36,13 @@ const SignupComponent: React.FC = () => {
|
|
|
36
36
|
getSignupService()
|
|
37
37
|
.initializeSignup(token || undefined)
|
|
38
38
|
.then((response) => {
|
|
39
|
-
if (active
|
|
39
|
+
if (!active) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!response.isReady && response.error) {
|
|
40
43
|
setInitializationError(response.error);
|
|
44
|
+
} else if (response.invite) {
|
|
45
|
+
setInviteEmail(response.invite.email);
|
|
41
46
|
}
|
|
42
47
|
})
|
|
43
48
|
.catch(() => {
|
|
@@ -66,15 +71,18 @@ const SignupComponent: React.FC = () => {
|
|
|
66
71
|
setError(undefined);
|
|
67
72
|
setBusy(true);
|
|
68
73
|
try {
|
|
69
|
-
// invited users don't enter an email (the invite token carries it)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
// invited users don't enter an email (the invite token carries it); the request also
|
|
75
|
+
// establishes the session (auto-login)
|
|
76
|
+
await new AuthApi().signup({ name, email: token ? undefined : email.trim(), password }, token || undefined);
|
|
77
|
+
} catch (error: any) {
|
|
78
|
+
setError(error.message);
|
|
73
79
|
setBusy(false);
|
|
74
80
|
return;
|
|
75
81
|
}
|
|
76
82
|
|
|
77
|
-
|
|
83
|
+
// Full navigation, not a router transition: the fresh page load renders under the
|
|
84
|
+
// just-established session, landing the new user in the app — never on the login form.
|
|
85
|
+
window.location.href = '/';
|
|
78
86
|
};
|
|
79
87
|
|
|
80
88
|
if (initializing) {
|
|
@@ -109,13 +117,20 @@ const SignupComponent: React.FC = () => {
|
|
|
109
117
|
<AuthLayout title='Create your account'>
|
|
110
118
|
<form onSubmit={onSubmit} noValidate>
|
|
111
119
|
<AuthTextField label='Name' value={name} onChange={setName} autoComplete='name' disabled={busy} />
|
|
112
|
-
{
|
|
120
|
+
{token ? (
|
|
121
|
+
// The invite fixes the email; render it read-only, tagged as the username. Without
|
|
122
|
+
// an email field here, password managers captured the NAME field as the saved
|
|
123
|
+
// credential's username — which then autofilled the login email field and failed.
|
|
124
|
+
<AuthTextField label='Email' value={inviteEmail} type='email' autoComplete='username' readOnly />
|
|
125
|
+
) : (
|
|
126
|
+
// 'username' (not 'email'): this is the credential identifier password managers
|
|
127
|
+
// save and fill; type='email' still gives the email keyboard.
|
|
113
128
|
<AuthTextField
|
|
114
129
|
label='Email'
|
|
115
130
|
value={email}
|
|
116
131
|
onChange={setEmail}
|
|
117
132
|
type='email'
|
|
118
|
-
autoComplete='
|
|
133
|
+
autoComplete='username'
|
|
119
134
|
disabled={busy}
|
|
120
135
|
/>
|
|
121
136
|
)}
|
package/test/authApi.test.ts
CHANGED
|
@@ -61,14 +61,28 @@ describe('AuthApi.initiatePasswordReset', () => {
|
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
describe('AuthApi.validateResetToken', () => {
|
|
64
|
-
it('
|
|
65
|
-
mockFetch({ body: { isValid: true } });
|
|
66
|
-
await expect(new AuthApi().validateResetToken('tok')).resolves.
|
|
64
|
+
it('resolves valid WITH the account email the reset is for (the form renders it as the identifier)', async () => {
|
|
65
|
+
mockFetch({ body: { isValid: true, email: 'ada@example.com' } });
|
|
66
|
+
await expect(new AuthApi().validateResetToken('tok')).resolves.toEqual({
|
|
67
|
+
valid: true,
|
|
68
|
+
email: 'ada@example.com',
|
|
69
|
+
});
|
|
67
70
|
});
|
|
68
71
|
|
|
69
|
-
it('
|
|
72
|
+
it('resolves invalid with the server message', async () => {
|
|
70
73
|
mockFetch({ body: { isValid: false, message: 'Token expired' } });
|
|
71
|
-
await expect(new AuthApi().validateResetToken('tok')).resolves.
|
|
74
|
+
await expect(new AuthApi().validateResetToken('tok')).resolves.toEqual({
|
|
75
|
+
valid: false,
|
|
76
|
+
message: 'Token expired',
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('falls back to a generic message when the server sends none', async () => {
|
|
81
|
+
mockFetch({ body: { isValid: false } });
|
|
82
|
+
await expect(new AuthApi().validateResetToken('tok')).resolves.toEqual({
|
|
83
|
+
valid: false,
|
|
84
|
+
message: 'Invalid or expired token',
|
|
85
|
+
});
|
|
72
86
|
});
|
|
73
87
|
});
|
|
74
88
|
|
package/test/authPages.test.tsx
CHANGED
|
@@ -21,8 +21,11 @@ describe('auth pages', () => {
|
|
|
21
21
|
// Real submit semantics: Enter submits the form.
|
|
22
22
|
expect(html).toContain('type="submit"');
|
|
23
23
|
// Mobile keyboards + autofill: proper autocomplete tokens (HTML attribute names are
|
|
24
|
-
// case-insensitive; SSR emits the camelCase form).
|
|
25
|
-
|
|
24
|
+
// case-insensitive; SSR emits the camelCase form). The identifier field is tagged
|
|
25
|
+
// `username` — the token password managers fill from the stored credential — while
|
|
26
|
+
// type="email" keeps the email keyboard.
|
|
27
|
+
expect(html.toLowerCase()).toContain('autocomplete="username"');
|
|
28
|
+
expect(html.toLowerCase()).toContain('type="email"');
|
|
26
29
|
expect(html.toLowerCase()).toContain('autocomplete="current-password"');
|
|
27
30
|
});
|
|
28
31
|
|
|
@@ -32,6 +35,8 @@ describe('auth pages', () => {
|
|
|
32
35
|
expect(html).toContain('Reset your password');
|
|
33
36
|
expect(html).toContain('Send reset link');
|
|
34
37
|
expect(html).toContain('href="/login"');
|
|
38
|
+
// Same identifier token as login, so the password manager fills the stored username here.
|
|
39
|
+
expect(html.toLowerCase()).toContain('autocomplete="username"');
|
|
35
40
|
});
|
|
36
41
|
|
|
37
42
|
it('password reset without a token renders the invalid-link state, never the form', () => {
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
* @jest-environment-options {"customExportConditions": ["node", "node-addons"]}
|
|
4
|
+
*
|
|
5
|
+
* Password-manager capture semantics on the password-reset form, through the real page
|
|
6
|
+
* component (token-validation effect included — global `fetch` is the only mock).
|
|
7
|
+
*
|
|
8
|
+
* The bug these tests pin down: the reset form rendered only the two password fields — no
|
|
9
|
+
* identifier at all — so password managers had nothing to associate the updated password
|
|
10
|
+
* with. The stored credential kept its OLD password while the account moved to the new one.
|
|
11
|
+
* The fix renders the account email (returned by token validation; the server knows it from
|
|
12
|
+
* the token's user row) as a read-only field tagged `autocomplete="username"` — read-only
|
|
13
|
+
* rather than disabled because password managers ignore disabled inputs.
|
|
14
|
+
*/
|
|
15
|
+
import React from 'react';
|
|
16
|
+
import { createRoot, Root } from 'react-dom/client';
|
|
17
|
+
import { act } from 'react-dom/test-utils';
|
|
18
|
+
import { passwordResetPage } from '../src/pages/PasswordReset';
|
|
19
|
+
|
|
20
|
+
declare global {
|
|
21
|
+
// eslint-disable-next-line no-var
|
|
22
|
+
var IS_REACT_ACT_ENVIRONMENT: boolean;
|
|
23
|
+
}
|
|
24
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
25
|
+
|
|
26
|
+
const ACCOUNT_EMAIL = 'ada@example.com';
|
|
27
|
+
|
|
28
|
+
let container: HTMLDivElement;
|
|
29
|
+
let root: Root;
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
container = document.createElement('div');
|
|
33
|
+
document.body.appendChild(container);
|
|
34
|
+
root = createRoot(container);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(async () => {
|
|
38
|
+
await act(async () => root.unmount());
|
|
39
|
+
container.remove();
|
|
40
|
+
delete (global as any).fetch;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function mockValidateResponse(body: unknown) {
|
|
44
|
+
(global as any).fetch = jest.fn().mockResolvedValue({
|
|
45
|
+
status: 200,
|
|
46
|
+
statusText: 'OK',
|
|
47
|
+
json: async () => body,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function renderPasswordReset(url: string) {
|
|
52
|
+
window.history.replaceState({}, '', url);
|
|
53
|
+
const PasswordReset = passwordResetPage.component;
|
|
54
|
+
await act(async () => {
|
|
55
|
+
root.render(<PasswordReset urlParams={{}} />);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function field(label: string): HTMLInputElement {
|
|
60
|
+
const input = document.getElementById(`auth-field-${label}`);
|
|
61
|
+
expect(input).not.toBeNull();
|
|
62
|
+
return input as HTMLInputElement;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe('password reset form', () => {
|
|
66
|
+
it('renders the account email as a read-only username field (password-manager capture target)', async () => {
|
|
67
|
+
mockValidateResponse({ isValid: true, email: ACCOUNT_EMAIL });
|
|
68
|
+
await renderPasswordReset('/login/reset-password?token=reset-token-1');
|
|
69
|
+
|
|
70
|
+
const email = field('Email');
|
|
71
|
+
expect(email.value).toBe(ACCOUNT_EMAIL);
|
|
72
|
+
expect(email.readOnly).toBe(true);
|
|
73
|
+
expect(email.getAttribute('autocomplete')).toBe('username');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('tags both password fields new-password so managers offer to update the credential', async () => {
|
|
77
|
+
mockValidateResponse({ isValid: true, email: ACCOUNT_EMAIL });
|
|
78
|
+
await renderPasswordReset('/login/reset-password?token=reset-token-1');
|
|
79
|
+
|
|
80
|
+
expect(field('New password').getAttribute('autocomplete')).toBe('new-password');
|
|
81
|
+
expect(field('Confirm new password').getAttribute('autocomplete')).toBe('new-password');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('an invalid token renders the invalid-link state — no form, no email field', async () => {
|
|
85
|
+
mockValidateResponse({ isValid: false, message: 'Token has expired' });
|
|
86
|
+
await renderPasswordReset('/login/reset-password?token=dead-token');
|
|
87
|
+
|
|
88
|
+
expect(document.body.textContent).toContain('This reset link is invalid');
|
|
89
|
+
expect(document.getElementById('auth-field-Email')).toBeNull();
|
|
90
|
+
expect(document.getElementById('auth-field-New password')).toBeNull();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
* @jest-environment-options {"customExportConditions": ["node", "node-addons"]}
|
|
4
|
+
*
|
|
5
|
+
* Password-manager capture semantics on the signup form, through the real page component
|
|
6
|
+
* (initialization effect included — `getSignupService` is the only mock).
|
|
7
|
+
*
|
|
8
|
+
* The bug these tests pin down: the invite path rendered NO email field, so browser password
|
|
9
|
+
* managers captured the closest text input — the NAME field — as the credential's username.
|
|
10
|
+
* The stored "Brent Test" credential then autofilled the login email field, producing
|
|
11
|
+
* "User name or password incorrect" against a perfectly valid password. The fix renders the
|
|
12
|
+
* invite's email (returned by `initializeSignup`, previously discarded) as a read-only field
|
|
13
|
+
* tagged `autocomplete="username"` — read-only rather than disabled because password managers
|
|
14
|
+
* ignore disabled inputs, which would recreate the bug.
|
|
15
|
+
*/
|
|
16
|
+
import React from 'react';
|
|
17
|
+
import { createRoot, Root } from 'react-dom/client';
|
|
18
|
+
import { act } from 'react-dom/test-utils';
|
|
19
|
+
import type { InitializeSignupResponse } from '@proteinjs/user';
|
|
20
|
+
import { signupPage } from '../src/pages/Signup';
|
|
21
|
+
|
|
22
|
+
declare global {
|
|
23
|
+
// eslint-disable-next-line no-var
|
|
24
|
+
var IS_REACT_ACT_ENVIRONMENT: boolean;
|
|
25
|
+
}
|
|
26
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
27
|
+
|
|
28
|
+
const initializeSignup = jest.fn<Promise<InitializeSignupResponse>, [string | undefined]>();
|
|
29
|
+
|
|
30
|
+
jest.mock('@proteinjs/user', () => ({
|
|
31
|
+
...jest.requireActual('@proteinjs/user'),
|
|
32
|
+
getSignupService: () => ({ initializeSignup }),
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
const INVITE_EMAIL = 'ada@example.com';
|
|
36
|
+
|
|
37
|
+
let container: HTMLDivElement;
|
|
38
|
+
let root: Root;
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
initializeSignup.mockReset();
|
|
42
|
+
container = document.createElement('div');
|
|
43
|
+
document.body.appendChild(container);
|
|
44
|
+
root = createRoot(container);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(async () => {
|
|
48
|
+
await act(async () => root.unmount());
|
|
49
|
+
container.remove();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
async function renderSignup(url: string) {
|
|
53
|
+
window.history.replaceState({}, '', url);
|
|
54
|
+
const Signup = signupPage.component;
|
|
55
|
+
await act(async () => {
|
|
56
|
+
root.render(<Signup urlParams={{}} />);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function field(label: string): HTMLInputElement {
|
|
61
|
+
const input = document.getElementById(`auth-field-${label}`);
|
|
62
|
+
expect(input).not.toBeNull();
|
|
63
|
+
return input as HTMLInputElement;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
describe('invited signup', () => {
|
|
67
|
+
beforeEach(() => {
|
|
68
|
+
initializeSignup.mockResolvedValue({
|
|
69
|
+
isReady: true,
|
|
70
|
+
isInviteOnly: true,
|
|
71
|
+
invite: { email: INVITE_EMAIL } as InitializeSignupResponse['invite'],
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('renders the invite email as a read-only username field (password-manager capture target)', async () => {
|
|
76
|
+
await renderSignup('/signup?token=invite-token-1');
|
|
77
|
+
expect(initializeSignup).toHaveBeenCalledWith('invite-token-1');
|
|
78
|
+
|
|
79
|
+
const email = field('Email');
|
|
80
|
+
expect(email.value).toBe(INVITE_EMAIL);
|
|
81
|
+
expect(email.readOnly).toBe(true);
|
|
82
|
+
expect(email.getAttribute('autocomplete')).toBe('username');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('keeps the name field out of credential capture (autocomplete name, never username)', async () => {
|
|
86
|
+
await renderSignup('/signup?token=invite-token-1');
|
|
87
|
+
|
|
88
|
+
const name = field('Name');
|
|
89
|
+
expect(name.readOnly).toBe(false);
|
|
90
|
+
expect(name.getAttribute('autocomplete')).toBe('name');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('tags both password fields new-password so managers offer to save the credential', async () => {
|
|
94
|
+
await renderSignup('/signup?token=invite-token-1');
|
|
95
|
+
|
|
96
|
+
expect(field('Password').getAttribute('autocomplete')).toBe('new-password');
|
|
97
|
+
expect(field('Confirm password').getAttribute('autocomplete')).toBe('new-password');
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('plain signup', () => {
|
|
102
|
+
beforeEach(() => {
|
|
103
|
+
initializeSignup.mockResolvedValue({ isReady: true, isInviteOnly: false });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('renders an editable email field tagged as the username', async () => {
|
|
107
|
+
await renderSignup('/signup');
|
|
108
|
+
expect(initializeSignup).toHaveBeenCalledWith(undefined);
|
|
109
|
+
|
|
110
|
+
const email = field('Email');
|
|
111
|
+
expect(email.readOnly).toBe(false);
|
|
112
|
+
expect(email.getAttribute('autocomplete')).toBe('username');
|
|
113
|
+
expect(field('Name').getAttribute('autocomplete')).toBe('name');
|
|
114
|
+
});
|
|
115
|
+
});
|