@proteinjs/user-ui 1.11.0 → 1.11.2

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +4 -1
  5. package/dist/index.js.map +1 -1
  6. package/dist/src/auth/AuthFormFields.d.ts +18 -0
  7. package/dist/src/auth/AuthFormFields.d.ts.map +1 -0
  8. package/dist/src/auth/AuthFormFields.js +41 -0
  9. package/dist/src/auth/AuthFormFields.js.map +1 -0
  10. package/dist/src/auth/AuthTextField.d.ts +14 -2
  11. package/dist/src/auth/AuthTextField.d.ts.map +1 -1
  12. package/dist/src/auth/AuthTextField.js +5 -3
  13. package/dist/src/auth/AuthTextField.js.map +1 -1
  14. package/dist/src/auth/AuthValidation.d.ts +17 -4
  15. package/dist/src/auth/AuthValidation.d.ts.map +1 -1
  16. package/dist/src/auth/AuthValidation.js +41 -11
  17. package/dist/src/auth/AuthValidation.js.map +1 -1
  18. package/dist/src/pages/ForgotPassword.d.ts.map +1 -1
  19. package/dist/src/pages/ForgotPassword.js +14 -4
  20. package/dist/src/pages/ForgotPassword.js.map +1 -1
  21. package/dist/src/pages/Login.d.ts.map +1 -1
  22. package/dist/src/pages/Login.js +40 -14
  23. package/dist/src/pages/Login.js.map +1 -1
  24. package/dist/src/pages/PasswordReset.d.ts.map +1 -1
  25. package/dist/src/pages/PasswordReset.js +37 -17
  26. package/dist/src/pages/PasswordReset.js.map +1 -1
  27. package/dist/src/pages/Signup.d.ts.map +1 -1
  28. package/dist/src/pages/Signup.js +44 -21
  29. package/dist/src/pages/Signup.js.map +1 -1
  30. package/dist/test/InviteRecordFormCustomization.test.js +2 -2
  31. package/dist/test/InviteRecordFormCustomization.test.js.map +1 -1
  32. package/dist/test/UserRecordFormCustomization.test.js +2 -2
  33. package/dist/test/UserRecordFormCustomization.test.js.map +1 -1
  34. package/dist/test/authFormsReadTheirFields.test.d.ts +5 -0
  35. package/dist/test/authFormsReadTheirFields.test.d.ts.map +1 -0
  36. package/dist/test/authFormsReadTheirFields.test.js +595 -0
  37. package/dist/test/authFormsReadTheirFields.test.js.map +1 -0
  38. package/dist/test/authValidation.test.js +43 -8
  39. package/dist/test/authValidation.test.js.map +1 -1
  40. package/index.ts +5 -1
  41. package/package.json +3 -3
  42. package/src/auth/AuthFormFields.ts +35 -0
  43. package/src/auth/AuthTextField.tsx +23 -3
  44. package/src/auth/AuthValidation.ts +54 -15
  45. package/src/pages/ForgotPassword.tsx +18 -5
  46. package/src/pages/Login.tsx +27 -8
  47. package/src/pages/PasswordReset.tsx +22 -11
  48. package/src/pages/Signup.tsx +39 -16
  49. package/test/InviteRecordFormCustomization.test.ts +2 -2
  50. package/test/UserRecordFormCustomization.test.tsx +2 -2
  51. package/test/authFormsReadTheirFields.test.tsx +369 -0
  52. package/test/authValidation.test.ts +49 -16
@@ -18,24 +18,55 @@ var validSignup = {
18
18
  password: 'hunter22',
19
19
  confirmPassword: 'hunter22',
20
20
  };
21
+ describe('AuthValidation.login', function () {
22
+ it('accepts an email and a password', function () {
23
+ expect(AuthValidation_1.AuthValidation.login({ email: 'ada@example.com', password: 'hunter22' })).toBeUndefined();
24
+ });
25
+ it('marks every blank field', function () {
26
+ expect(AuthValidation_1.AuthValidation.login({ email: '', password: '' })).toEqual({
27
+ email: 'Enter your email',
28
+ password: 'Enter your password',
29
+ });
30
+ });
31
+ it('treats a whitespace-only email as blank, and marks only the blank field', function () {
32
+ expect(AuthValidation_1.AuthValidation.login({ email: ' ', password: 'hunter22' })).toEqual({ email: 'Enter your email' });
33
+ });
34
+ });
35
+ describe('AuthValidation.forgotPassword', function () {
36
+ it('accepts an email', function () {
37
+ expect(AuthValidation_1.AuthValidation.forgotPassword({ email: 'ada@example.com' })).toBeUndefined();
38
+ });
39
+ it('marks a blank email', function () {
40
+ expect(AuthValidation_1.AuthValidation.forgotPassword({ email: ' ' })).toEqual({ email: 'Enter your email' });
41
+ });
42
+ });
21
43
  describe('AuthValidation.signup', function () {
22
44
  it('accepts a complete, matching signup', function () {
23
45
  expect(AuthValidation_1.AuthValidation.signup(validSignup, false)).toBeUndefined();
24
46
  });
25
47
  it('requires a name', function () {
26
- expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { name: ' ' }), false)).toBe('Please enter your name.');
48
+ expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { name: ' ' }), false)).toEqual({ name: 'Enter your name' });
49
+ });
50
+ it('requires an email when not invited', function () {
51
+ expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { email: '' }), false)).toEqual({ email: 'Enter your email' });
27
52
  });
28
53
  it('requires a valid email when not invited', function () {
29
- expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { email: 'not-an-email' }), false)).toBe('Please enter a valid email address.');
54
+ expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { email: 'not-an-email' }), false)).toEqual({
55
+ email: 'Enter a valid email address',
56
+ });
30
57
  });
31
58
  it('skips the email check for invited users (the invite token carries the email)', function () {
32
59
  expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { email: '' }), true)).toBeUndefined();
33
60
  });
34
61
  it('requires a password', function () {
35
- expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { password: '', confirmPassword: '' }), false)).toBe('Please enter a password.');
62
+ expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { password: '', confirmPassword: '' }), false)).toEqual({
63
+ password: 'Enter a password',
64
+ });
36
65
  });
37
- it('rejects a mismatched password confirmation', function () {
38
- expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { confirmPassword: 'different' }), false)).toBe('Passwords do not match.');
66
+ it('marks a mismatched password confirmation on the confirmation', function () {
67
+ expect(AuthValidation_1.AuthValidation.signup(__assign(__assign({}, validSignup), { confirmPassword: 'different' }), false)).toEqual({
68
+ confirmPassword: 'Passwords do not match',
69
+ });
39
70
  });
40
71
  });
41
72
  describe('AuthValidation.passwordReset', function () {
@@ -43,10 +74,14 @@ describe('AuthValidation.passwordReset', function () {
43
74
  expect(AuthValidation_1.AuthValidation.passwordReset({ newPassword: 'hunter22', confirmPassword: 'hunter22' })).toBeUndefined();
44
75
  });
45
76
  it('requires a new password', function () {
46
- expect(AuthValidation_1.AuthValidation.passwordReset({ newPassword: '', confirmPassword: '' })).toBe('Please enter a new password.');
77
+ expect(AuthValidation_1.AuthValidation.passwordReset({ newPassword: '', confirmPassword: '' })).toEqual({
78
+ newPassword: 'Enter a new password',
79
+ });
47
80
  });
48
- it('rejects a mismatched confirmation', function () {
49
- expect(AuthValidation_1.AuthValidation.passwordReset({ newPassword: 'hunter22', confirmPassword: 'hunter23' })).toBe('Passwords do not match.');
81
+ it('marks a mismatched confirmation on the confirmation', function () {
82
+ expect(AuthValidation_1.AuthValidation.passwordReset({ newPassword: 'hunter22', confirmPassword: 'hunter23' })).toEqual({
83
+ confirmPassword: 'Passwords do not match',
84
+ });
50
85
  });
51
86
  });
52
87
  //# sourceMappingURL=authValidation.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"authValidation.test.js","sourceRoot":"","sources":["../../test/authValidation.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,6DAA4D;AAE5D,IAAM,WAAW,GAAG;IAClB,IAAI,EAAE,cAAc;IACpB,KAAK,EAAE,iBAAiB;IACxB,QAAQ,EAAE,UAAU;IACpB,eAAe,EAAE,UAAU;CAC5B,CAAC;AAEF,QAAQ,CAAC,uBAAuB,EAAE;IAChC,EAAE,CAAC,qCAAqC,EAAE;QACxC,MAAM,CAAC,+BAAc,CAAC,MAAM,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;IACpE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iBAAiB,EAAE;QACpB,MAAM,CAA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C;YACrF,WAAW,EAAE,sBAAsB;SACpC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qDAAqD,EAAE;QACxD,MAAM,CAAC,+BAAc,CAAC,aAAa,CAAC,EAAE,WAAW,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;YACrG,eAAe,EAAE,wBAAwB;SAC1C,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/index.ts CHANGED
@@ -4,9 +4,13 @@ export * from './src/presence/UserPresenceReporter';
4
4
  export * from './src/presence/installUserPresenceReporter';
5
5
 
6
6
  // The logged-out surface's building blocks, exported so a consumer app's own public pages
7
- // (e.g. n3xa's /request-invite) render with the same auth look instead of forking the styling.
7
+ // (e.g. a /request-invite page) render with the same auth look instead of forking the styling.
8
8
  export * from './src/auth/AuthLayout';
9
9
  export * from './src/auth/AuthTextField';
10
10
  export * from './src/auth/AuthButton';
11
11
  export * from './src/auth/AuthFormError';
12
12
  export * from './src/auth/AuthMessagePanel';
13
+ // The form reader the pages above use at submit (uncontrolled fields; the values read from the
14
+ // form itself) — exported so a consumer page fixes the same class the same way, not by hand.
15
+ export * from './src/auth/AuthFormFields';
16
+ export type { AuthFieldErrors } from './src/auth/AuthValidation';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proteinjs/user-ui",
3
- "version": "1.11.0",
3
+ "version": "1.11.2",
4
4
  "description": "User ui components",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -29,7 +29,7 @@
29
29
  "@proteinjs/reflection": "^1.2.0",
30
30
  "@proteinjs/server-api": "^3.0.11",
31
31
  "@proteinjs/ui": "^4.14.0",
32
- "@proteinjs/user": "^1.20.0",
32
+ "@proteinjs/user": "^1.20.1",
33
33
  "@proteinjs/util": "^1.6.1",
34
34
  "moment": "2.29.4",
35
35
  "react": "18.2.0",
@@ -56,5 +56,5 @@
56
56
  },
57
57
  "main": "./dist/generated/index.js",
58
58
  "types": "./dist/generated/index.d.ts",
59
- "gitHead": "b41385de00be0c41a62e4aa1d37b0cf56907d192"
59
+ "gitHead": "0de51035863a8f78bf65dde71bf449e51adf8779"
60
60
  }
@@ -0,0 +1,35 @@
1
+ import { AuthFieldErrors } from './AuthValidation';
2
+
3
+ /**
4
+ * Reads an auth form's fields from the form itself when it submits.
5
+ *
6
+ * A password manager, the platform's autofill service or an input method can write a field's
7
+ * value without the `input` event React listens for: the field looks filled while component
8
+ * state is still empty, and a controlled field's next re-render (the focus ring moving as the
9
+ * person taps the button is enough) writes that empty state back over the value. So the fields
10
+ * a person fills are uncontrolled — each carries a `name` and owns its value in the DOM — and
11
+ * the submit handler reads them here. There is no second copy of a value to fall out of step.
12
+ */
13
+ export class AuthFormFields {
14
+ /** The named fields' current values; '' for a name the form does not carry. */
15
+ static read<F extends string>(form: HTMLFormElement, names: readonly F[]): Record<F, string> {
16
+ const data = new FormData(form);
17
+ const values = {} as Record<F, string>;
18
+ for (const name of names) {
19
+ const value = data.get(name);
20
+ values[name] = typeof value === 'string' ? value : '';
21
+ }
22
+ return values;
23
+ }
24
+
25
+ /** Focuses the first field, in document order, that carries an error (the keyboard opens there). */
26
+ static focusFirstInvalid<F extends string>(form: HTMLFormElement, errors: AuthFieldErrors<F>) {
27
+ for (let index = 0; index < form.elements.length; index++) {
28
+ const element = form.elements[index] as HTMLInputElement;
29
+ if (element.name && errors[element.name as F]) {
30
+ element.focus();
31
+ return;
32
+ }
33
+ }
34
+ }
35
+ }
@@ -5,9 +5,21 @@ import VisibilityOff from '@mui/icons-material/VisibilityOff';
5
5
 
6
6
  export interface AuthTextFieldProps {
7
7
  label: string;
8
- value: string;
9
- /** Omitted for read-only fields. */
8
+ /**
9
+ * The form-field name the page reads the value by when the form submits
10
+ * (`AuthFormFields.read`). A field the person fills is uncontrolled — it carries `name` (and an
11
+ * optional `defaultValue`) and no `value` — so whatever writes into it (typing, a password
12
+ * manager, the platform's autofill) is exactly what the submit reads.
13
+ */
14
+ name?: string;
15
+ /** Controlled value — for a field the page itself fills (e.g. a read-only identity). */
16
+ value?: string;
17
+ /** Initial value of an uncontrolled field. */
18
+ defaultValue?: string;
19
+ /** Called on every edit the browser announces; an uncontrolled field's value stays in the DOM. */
10
20
  onChange?: (value: string) => void;
21
+ /** Marks the field invalid and shows this message under it (in place of `helperText`). */
22
+ error?: string;
11
23
  /** Renders a reveal toggle and masks input until toggled. */
12
24
  password?: boolean;
13
25
  /**
@@ -36,8 +48,11 @@ export interface AuthTextFieldProps {
36
48
  export function AuthTextField(props: AuthTextFieldProps) {
37
49
  const {
38
50
  label,
51
+ name,
39
52
  value,
53
+ defaultValue,
40
54
  onChange,
55
+ error,
41
56
  password,
42
57
  readOnly,
43
58
  autoComplete,
@@ -61,7 +76,9 @@ export function AuthTextField(props: AuthTextFieldProps) {
61
76
  <TextField
62
77
  id={`auth-field-${label}`}
63
78
  fullWidth
79
+ name={name}
64
80
  value={value}
81
+ defaultValue={defaultValue}
65
82
  onChange={(event) => onChange?.(event.target.value)}
66
83
  type={password && !revealed ? 'password' : password ? 'text' : type || 'text'}
67
84
  autoComplete={autoComplete}
@@ -69,7 +86,8 @@ export function AuthTextField(props: AuthTextFieldProps) {
69
86
  disabled={disabled}
70
87
  multiline={!!multilineRows}
71
88
  rows={multilineRows}
72
- helperText={helperText}
89
+ error={!!error}
90
+ helperText={error ?? helperText}
73
91
  InputProps={{
74
92
  readOnly,
75
93
  endAdornment: password ? (
@@ -98,6 +116,8 @@ export function AuthTextField(props: AuthTextFieldProps) {
98
116
  '& fieldset': { borderColor: 'divider' },
99
117
  '&:hover fieldset': { borderColor: readOnly ? 'divider' : 'text.disabled' },
100
118
  '&.Mui-focused fieldset': { borderColor: 'primary.main', borderWidth: '1.5px' },
119
+ // A marked field keeps its error border while focused (the person is fixing it).
120
+ '&.Mui-error fieldset': { borderColor: 'error.main' },
101
121
  },
102
122
  }}
103
123
  />
@@ -1,39 +1,78 @@
1
1
  import { emailRegex } from '@proteinjs/util';
2
2
 
3
+ /** A user-readable message per invalid field, keyed by the field's form name. */
4
+ export type AuthFieldErrors<F extends string> = { [K in F]?: string };
5
+
3
6
  /**
4
- * Client-side validation for the auth flows. Each method returns a user-readable
5
- * error message, or undefined when the input is valid.
7
+ * Client-side validation for the auth flows. Each method returns the invalid fields with a
8
+ * user-readable message each (the page marks those fields), or undefined when the input is
9
+ * valid. A blank field is refused here, in the page, so a real form never sends one.
6
10
  */
7
11
  export class AuthValidation {
12
+ static login(fields: { email: string; password: string }): AuthFieldErrors<'email' | 'password'> | undefined {
13
+ const errors: AuthFieldErrors<'email' | 'password'> = {};
14
+ if (!fields.email.trim()) {
15
+ errors.email = 'Enter your email';
16
+ }
17
+
18
+ if (!fields.password) {
19
+ errors.password = 'Enter your password';
20
+ }
21
+
22
+ return AuthValidation.outcome(errors);
23
+ }
24
+
25
+ static forgotPassword(fields: { email: string }): AuthFieldErrors<'email'> | undefined {
26
+ const errors: AuthFieldErrors<'email'> = {};
27
+ if (!fields.email.trim()) {
28
+ errors.email = 'Enter your email';
29
+ }
30
+
31
+ return AuthValidation.outcome(errors);
32
+ }
33
+
8
34
  /** @param invited invited users don't enter an email (the invite token carries it) */
9
35
  static signup(
10
36
  fields: { name: string; email: string; password: string; confirmPassword: string },
11
37
  invited: boolean
12
- ): string | undefined {
38
+ ): AuthFieldErrors<'name' | 'email' | 'password' | 'confirmPassword'> | undefined {
39
+ const errors: AuthFieldErrors<'name' | 'email' | 'password' | 'confirmPassword'> = {};
13
40
  if (!fields.name.trim()) {
14
- return 'Please enter your name.';
41
+ errors.name = 'Enter your name';
15
42
  }
16
43
 
17
- if (!invited && !emailRegex.test(fields.email.trim())) {
18
- return 'Please enter a valid email address.';
44
+ if (!invited) {
45
+ if (!fields.email.trim()) {
46
+ errors.email = 'Enter your email';
47
+ } else if (!emailRegex.test(fields.email.trim())) {
48
+ errors.email = 'Enter a valid email address';
49
+ }
19
50
  }
20
51
 
21
52
  if (!fields.password) {
22
- return 'Please enter a password.';
53
+ errors.password = 'Enter a password';
54
+ } else if (fields.password !== fields.confirmPassword) {
55
+ errors.confirmPassword = 'Passwords do not match';
23
56
  }
24
57
 
25
- if (fields.password !== fields.confirmPassword) {
26
- return 'Passwords do not match.';
27
- }
58
+ return AuthValidation.outcome(errors);
28
59
  }
29
60
 
30
- static passwordReset(fields: { newPassword: string; confirmPassword: string }): string | undefined {
61
+ static passwordReset(fields: {
62
+ newPassword: string;
63
+ confirmPassword: string;
64
+ }): AuthFieldErrors<'newPassword' | 'confirmPassword'> | undefined {
65
+ const errors: AuthFieldErrors<'newPassword' | 'confirmPassword'> = {};
31
66
  if (!fields.newPassword) {
32
- return 'Please enter a new password.';
67
+ errors.newPassword = 'Enter a new password';
68
+ } else if (fields.newPassword !== fields.confirmPassword) {
69
+ errors.confirmPassword = 'Passwords do not match';
33
70
  }
34
71
 
35
- if (fields.newPassword !== fields.confirmPassword) {
36
- return 'Passwords do not match.';
37
- }
72
+ return AuthValidation.outcome(errors);
73
+ }
74
+
75
+ private static outcome<F extends string>(errors: AuthFieldErrors<F>): AuthFieldErrors<F> | undefined {
76
+ return Object.keys(errors).length > 0 ? errors : undefined;
38
77
  }
39
78
  }
@@ -9,19 +9,31 @@ import { AuthButton } from '../auth/AuthButton';
9
9
  import { AuthFormError } from '../auth/AuthFormError';
10
10
  import { AuthMessagePanel } from '../auth/AuthMessagePanel';
11
11
  import { AuthApi } from '../auth/AuthApi';
12
+ import { AuthFormFields } from '../auth/AuthFormFields';
13
+ import { AuthFieldErrors, AuthValidation } from '../auth/AuthValidation';
12
14
 
13
15
  const ForgotPasswordComponent: React.FC = () => {
14
- const [email, setEmail] = useState('');
16
+ const [fieldErrors, setFieldErrors] = useState<AuthFieldErrors<'email'>>({});
15
17
  const [error, setError] = useState<string | undefined>();
16
18
  const [busy, setBusy] = useState(false);
17
19
  const [sent, setSent] = useState(false);
18
20
 
19
- const onSubmit = async (event: React.FormEvent) => {
21
+ const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
20
22
  event.preventDefault();
23
+ // Read from the form at submit, like login: an autofilled email may be in the DOM only.
24
+ const form = event.currentTarget;
25
+ const { email } = AuthFormFields.read(form, ['email'] as const);
26
+ const invalid = AuthValidation.forgotPassword({ email });
27
+ setFieldErrors(invalid ?? {});
28
+ if (invalid) {
29
+ AuthFormFields.focusFirstInvalid(form, invalid);
30
+ return;
31
+ }
32
+
21
33
  setError(undefined);
22
34
  setBusy(true);
23
35
  try {
24
- await new AuthApi().initiatePasswordReset(email);
36
+ await new AuthApi().initiatePasswordReset(email.trim());
25
37
  setSent(true);
26
38
  } catch (error: any) {
27
39
  setError(error.message);
@@ -54,8 +66,9 @@ const ForgotPasswordComponent: React.FC = () => {
54
66
  stored username here too. */}
55
67
  <AuthTextField
56
68
  label='Email'
57
- value={email}
58
- onChange={setEmail}
69
+ name='email'
70
+ onChange={() => setFieldErrors((errors) => (errors.email ? {} : errors))}
71
+ error={fieldErrors.email}
59
72
  type='email'
60
73
  autoComplete='username'
61
74
  disabled={busy}
@@ -8,19 +8,36 @@ import { AuthTextField } from '../auth/AuthTextField';
8
8
  import { AuthButton } from '../auth/AuthButton';
9
9
  import { AuthFormError } from '../auth/AuthFormError';
10
10
  import { AuthApi } from '../auth/AuthApi';
11
+ import { AuthFormFields } from '../auth/AuthFormFields';
12
+ import { AuthFieldErrors, AuthValidation } from '../auth/AuthValidation';
13
+
14
+ type LoginField = 'email' | 'password';
11
15
 
12
16
  const LoginComponent: React.FC = () => {
13
- const [email, setEmail] = useState('');
14
- const [password, setPassword] = useState('');
17
+ const [fieldErrors, setFieldErrors] = useState<AuthFieldErrors<LoginField>>({});
15
18
  const [error, setError] = useState<string | undefined>();
16
19
  const [busy, setBusy] = useState(false);
17
20
 
18
- const onSubmit = async (event: React.FormEvent) => {
21
+ const clearFieldError = (name: LoginField) =>
22
+ setFieldErrors((errors) => (errors[name] ? { ...errors, [name]: undefined } : errors));
23
+
24
+ const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
19
25
  event.preventDefault();
26
+ // The fields' values as they are NOW, read from the form — a password manager or autofill
27
+ // that wrote a field without an input event is only in the DOM (AuthFormFields).
28
+ const form = event.currentTarget;
29
+ const { email, password } = AuthFormFields.read(form, ['email', 'password'] as const);
30
+ const invalid = AuthValidation.login({ email, password });
31
+ setFieldErrors(invalid ?? {});
32
+ if (invalid) {
33
+ AuthFormFields.focusFirstInvalid(form, invalid);
34
+ return;
35
+ }
36
+
20
37
  setError(undefined);
21
38
  setBusy(true);
22
39
  try {
23
- await new AuthApi().login(email, password);
40
+ await new AuthApi().login(email.trim(), password);
24
41
  window.location.href = '/';
25
42
  } catch (error: any) {
26
43
  setError(error.message);
@@ -39,16 +56,18 @@ const LoginComponent: React.FC = () => {
39
56
  fill from the saved login; type='email' still gives the email keyboard. */}
40
57
  <AuthTextField
41
58
  label='Email'
42
- value={email}
43
- onChange={setEmail}
59
+ name='email'
60
+ onChange={() => clearFieldError('email')}
61
+ error={fieldErrors.email}
44
62
  type='email'
45
63
  autoComplete='username'
46
64
  disabled={busy}
47
65
  />
48
66
  <AuthTextField
49
67
  label='Password'
50
- value={password}
51
- onChange={setPassword}
68
+ name='password'
69
+ onChange={() => clearFieldError('password')}
70
+ error={fieldErrors.password}
52
71
  password
53
72
  autoComplete='current-password'
54
73
  disabled={busy}
@@ -9,7 +9,10 @@ import { AuthButton } from '../auth/AuthButton';
9
9
  import { AuthFormError } from '../auth/AuthFormError';
10
10
  import { AuthMessagePanel } from '../auth/AuthMessagePanel';
11
11
  import { AuthApi } from '../auth/AuthApi';
12
- import { AuthValidation } from '../auth/AuthValidation';
12
+ import { AuthFormFields } from '../auth/AuthFormFields';
13
+ import { AuthFieldErrors, AuthValidation } from '../auth/AuthValidation';
14
+
15
+ type PasswordResetField = 'newPassword' | 'confirmPassword';
13
16
 
14
17
  function resetTokenFromUrl(): string {
15
18
  if (typeof window === 'undefined') {
@@ -26,8 +29,7 @@ const PasswordResetComponent: React.FC = () => {
26
29
  const [validating, setValidating] = useState(!!token);
27
30
  const [invalid, setInvalid] = useState(!token);
28
31
  const [email, setEmail] = useState('');
29
- const [newPassword, setNewPassword] = useState('');
30
- const [confirmPassword, setConfirmPassword] = useState('');
32
+ const [fieldErrors, setFieldErrors] = useState<AuthFieldErrors<PasswordResetField>>({});
31
33
  const [error, setError] = useState<string | undefined>();
32
34
  const [busy, setBusy] = useState(false);
33
35
  const [resetComplete, setResetComplete] = useState(false);
@@ -65,11 +67,18 @@ const PasswordResetComponent: React.FC = () => {
65
67
  };
66
68
  }, [token]);
67
69
 
68
- const onSubmit = async (event: React.FormEvent) => {
70
+ const clearFieldError = (name: PasswordResetField) =>
71
+ setFieldErrors((errors) => (errors[name] ? { ...errors, [name]: undefined } : errors));
72
+
73
+ const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
69
74
  event.preventDefault();
70
- const validationError = AuthValidation.passwordReset({ newPassword, confirmPassword });
71
- if (validationError) {
72
- setError(validationError);
75
+ // Read from the form at submit: a suggested or saved password may be in the DOM only.
76
+ const form = event.currentTarget;
77
+ const { newPassword, confirmPassword } = AuthFormFields.read(form, ['newPassword', 'confirmPassword'] as const);
78
+ const invalid = AuthValidation.passwordReset({ newPassword, confirmPassword });
79
+ setFieldErrors(invalid ?? {});
80
+ if (invalid) {
81
+ AuthFormFields.focusFirstInvalid(form, invalid);
73
82
  return;
74
83
  }
75
84
 
@@ -130,16 +139,18 @@ const PasswordResetComponent: React.FC = () => {
130
139
  <AuthTextField label='Email' value={email} type='email' autoComplete='username' readOnly />
131
140
  <AuthTextField
132
141
  label='New password'
133
- value={newPassword}
134
- onChange={setNewPassword}
142
+ name='newPassword'
143
+ onChange={() => clearFieldError('newPassword')}
144
+ error={fieldErrors.newPassword}
135
145
  password
136
146
  autoComplete='new-password'
137
147
  disabled={busy}
138
148
  />
139
149
  <AuthTextField
140
150
  label='Confirm new password'
141
- value={confirmPassword}
142
- onChange={setConfirmPassword}
151
+ name='confirmPassword'
152
+ onChange={() => clearFieldError('confirmPassword')}
153
+ error={fieldErrors.confirmPassword}
143
154
  password
144
155
  autoComplete='new-password'
145
156
  disabled={busy}
@@ -8,8 +8,11 @@ import { AuthTextField } from '../auth/AuthTextField';
8
8
  import { AuthButton } from '../auth/AuthButton';
9
9
  import { AuthFormError } from '../auth/AuthFormError';
10
10
  import { AuthMessagePanel } from '../auth/AuthMessagePanel';
11
- import { AuthValidation } from '../auth/AuthValidation';
11
+ import { AuthFieldErrors, AuthValidation } from '../auth/AuthValidation';
12
12
  import { AuthApi } from '../auth/AuthApi';
13
+ import { AuthFormFields } from '../auth/AuthFormFields';
14
+
15
+ type SignupField = 'name' | 'email' | 'password' | 'confirmPassword';
13
16
 
14
17
  function inviteTokenFromUrl(): string {
15
18
  if (typeof window === 'undefined') {
@@ -23,11 +26,8 @@ const SignupComponent: React.FC = () => {
23
26
  const [token] = useState(inviteTokenFromUrl);
24
27
  const [initializing, setInitializing] = useState(true);
25
28
  const [initializationError, setInitializationError] = useState<string | null>(null);
26
- const [name, setName] = useState('');
27
- const [email, setEmail] = useState('');
28
29
  const [inviteEmail, setInviteEmail] = useState('');
29
- const [password, setPassword] = useState('');
30
- const [confirmPassword, setConfirmPassword] = useState('');
30
+ const [fieldErrors, setFieldErrors] = useState<AuthFieldErrors<SignupField>>({});
31
31
  const [error, setError] = useState<string | undefined>();
32
32
  const [busy, setBusy] = useState(false);
33
33
 
@@ -60,11 +60,24 @@ const SignupComponent: React.FC = () => {
60
60
  };
61
61
  }, [token]);
62
62
 
63
- const onSubmit = async (event: React.FormEvent) => {
63
+ const clearFieldError = (name: SignupField) =>
64
+ setFieldErrors((errors) => (errors[name] ? { ...errors, [name]: undefined } : errors));
65
+
66
+ const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
64
67
  event.preventDefault();
65
- const validationError = AuthValidation.signup({ name, email, password, confirmPassword }, !!token);
66
- if (validationError) {
67
- setError(validationError);
68
+ // Read from the form at submit: a password manager's suggested password (or any autofill)
69
+ // may be in the DOM only. The invite path carries no email field; its read is ''.
70
+ const form = event.currentTarget;
71
+ const { name, email, password, confirmPassword } = AuthFormFields.read(form, [
72
+ 'name',
73
+ 'email',
74
+ 'password',
75
+ 'confirmPassword',
76
+ ] as const);
77
+ const invalid = AuthValidation.signup({ name, email, password, confirmPassword }, !!token);
78
+ setFieldErrors(invalid ?? {});
79
+ if (invalid) {
80
+ AuthFormFields.focusFirstInvalid(form, invalid);
68
81
  return;
69
82
  }
70
83
 
@@ -116,7 +129,14 @@ const SignupComponent: React.FC = () => {
116
129
  </Helmet>
117
130
  <AuthLayout title='Create your account'>
118
131
  <form onSubmit={onSubmit} noValidate>
119
- <AuthTextField label='Name' value={name} onChange={setName} autoComplete='name' disabled={busy} />
132
+ <AuthTextField
133
+ label='Name'
134
+ name='name'
135
+ onChange={() => clearFieldError('name')}
136
+ error={fieldErrors.name}
137
+ autoComplete='name'
138
+ disabled={busy}
139
+ />
120
140
  {token ? (
121
141
  // The invite fixes the email; render it read-only, tagged as the username. Without
122
142
  // an email field here, password managers captured the NAME field as the saved
@@ -127,8 +147,9 @@ const SignupComponent: React.FC = () => {
127
147
  // save and fill; type='email' still gives the email keyboard.
128
148
  <AuthTextField
129
149
  label='Email'
130
- value={email}
131
- onChange={setEmail}
150
+ name='email'
151
+ onChange={() => clearFieldError('email')}
152
+ error={fieldErrors.email}
132
153
  type='email'
133
154
  autoComplete='username'
134
155
  disabled={busy}
@@ -136,16 +157,18 @@ const SignupComponent: React.FC = () => {
136
157
  )}
137
158
  <AuthTextField
138
159
  label='Password'
139
- value={password}
140
- onChange={setPassword}
160
+ name='password'
161
+ onChange={() => clearFieldError('password')}
162
+ error={fieldErrors.password}
141
163
  password
142
164
  autoComplete='new-password'
143
165
  disabled={busy}
144
166
  />
145
167
  <AuthTextField
146
168
  label='Confirm password'
147
- value={confirmPassword}
148
- onChange={setConfirmPassword}
169
+ name='confirmPassword'
170
+ onChange={() => clearFieldError('confirmPassword')}
171
+ error={fieldErrors.confirmPassword}
149
172
  password
150
173
  autoComplete='new-password'
151
174
  disabled={busy}
@@ -19,7 +19,7 @@ type UserAuthInternals = {
19
19
 
20
20
  const setRoles = (roles: string[]) => {
21
21
  (UserAuth as unknown as UserAuthInternals).userRepo = {
22
- getUser: () => ({ email: 'someone@n3xa.io', roles }),
22
+ getUser: () => ({ email: 'someone@example.com', roles }),
23
23
  };
24
24
  };
25
25
 
@@ -36,7 +36,7 @@ const defaultFormButtons = (): FormButtons<any> => ({
36
36
  });
37
37
 
38
38
  const defaultFieldLayout = ['email', 'token', 'tokenExpiresAt', 'invitedBy'];
39
- const existingInvite = { id: 'invite-1', email: 'invitee@n3xa.io' } as Invite;
39
+ const existingInvite = { id: 'invite-1', email: 'invitee@example.com' } as Invite;
40
40
 
41
41
  const visibleButtons = (record: Invite | undefined) => {
42
42
  const buttons = new InviteRecordFormCustomization().getFormButtons(record, defaultFormButtons());
@@ -52,7 +52,7 @@ type UserAuthInternals = {
52
52
 
53
53
  const setCallerRoles = (roles: string[]) => {
54
54
  (UserAuth as unknown as UserAuthInternals).userRepo = {
55
- getUser: () => ({ email: 'caller@n3xa.io', roles }),
55
+ getUser: () => ({ email: 'caller@example.com', roles }),
56
56
  };
57
57
  };
58
58
 
@@ -72,7 +72,7 @@ const userRecord = (overrides: Partial<User> = {}): User =>
72
72
  ({
73
73
  id: 'user-1',
74
74
  name: 'Ada',
75
- email: 'ada@n3xa.io',
75
+ email: 'ada@example.com',
76
76
  roles: ['ops'],
77
77
  status: 'active',
78
78
  ...overrides,