@proteinjs/user-ui 1.4.1 → 1.5.0

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 (75) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/LICENSE +21 -0
  3. package/dist/generated/index.d.ts +0 -9
  4. package/dist/generated/index.d.ts.map +1 -1
  5. package/dist/generated/index.js +1 -10
  6. package/dist/generated/index.js.map +1 -1
  7. package/dist/src/auth/AuthApi.d.ts +12 -0
  8. package/dist/src/auth/AuthApi.d.ts.map +1 -0
  9. package/dist/src/auth/AuthApi.js +151 -0
  10. package/dist/src/auth/AuthApi.js.map +1 -0
  11. package/dist/src/auth/AuthButton.d.ts +13 -0
  12. package/dist/src/auth/AuthButton.d.ts.map +1 -0
  13. package/dist/src/auth/AuthButton.js +21 -0
  14. package/dist/src/auth/AuthButton.js.map +1 -0
  15. package/dist/src/auth/AuthFormError.d.ts +6 -0
  16. package/dist/src/auth/AuthFormError.d.ts.map +1 -0
  17. package/dist/src/auth/AuthFormError.js +18 -0
  18. package/dist/src/auth/AuthFormError.js.map +1 -0
  19. package/dist/src/auth/AuthLayout.d.ts +17 -0
  20. package/dist/src/auth/AuthLayout.d.ts.map +1 -0
  21. package/dist/src/auth/AuthLayout.js +52 -0
  22. package/dist/src/auth/AuthLayout.js.map +1 -0
  23. package/dist/src/auth/AuthMessagePanel.d.ts +13 -0
  24. package/dist/src/auth/AuthMessagePanel.d.ts.map +1 -0
  25. package/dist/src/auth/AuthMessagePanel.js +22 -0
  26. package/dist/src/auth/AuthMessagePanel.js.map +1 -0
  27. package/dist/src/auth/AuthTextField.d.ts +21 -0
  28. package/dist/src/auth/AuthTextField.d.ts.map +1 -0
  29. package/dist/src/auth/AuthTextField.js +60 -0
  30. package/dist/src/auth/AuthTextField.js.map +1 -0
  31. package/dist/src/auth/AuthValidation.d.ts +18 -0
  32. package/dist/src/auth/AuthValidation.d.ts.map +1 -0
  33. package/dist/src/auth/AuthValidation.js +38 -0
  34. package/dist/src/auth/AuthValidation.js.map +1 -0
  35. package/dist/src/pages/ForgotPassword.d.ts.map +1 -1
  36. package/dist/src/pages/ForgotPassword.js +77 -75
  37. package/dist/src/pages/ForgotPassword.js.map +1 -1
  38. package/dist/src/pages/Login.d.ts.map +1 -1
  39. package/dist/src/pages/Login.js +79 -97
  40. package/dist/src/pages/Login.js.map +1 -1
  41. package/dist/src/pages/PasswordReset.d.ts.map +1 -1
  42. package/dist/src/pages/PasswordReset.js +86 -134
  43. package/dist/src/pages/PasswordReset.js.map +1 -1
  44. package/dist/src/pages/Signup.d.ts.map +1 -1
  45. package/dist/src/pages/Signup.js +88 -148
  46. package/dist/src/pages/Signup.js.map +1 -1
  47. package/dist/test/authApi.test.d.ts +2 -0
  48. package/dist/test/authApi.test.d.ts.map +1 -0
  49. package/dist/test/authApi.test.js +188 -0
  50. package/dist/test/authApi.test.js.map +1 -0
  51. package/dist/test/authPages.test.d.ts +2 -0
  52. package/dist/test/authPages.test.d.ts.map +1 -0
  53. package/dist/test/authPages.test.js +52 -0
  54. package/dist/test/authPages.test.js.map +1 -0
  55. package/dist/test/authValidation.test.d.ts +2 -0
  56. package/dist/test/authValidation.test.d.ts.map +1 -0
  57. package/dist/test/authValidation.test.js +52 -0
  58. package/dist/test/authValidation.test.js.map +1 -0
  59. package/generated/index.ts +8 -20
  60. package/package.json +4 -3
  61. package/src/auth/AuthApi.ts +67 -0
  62. package/src/auth/AuthButton.tsx +38 -0
  63. package/src/auth/AuthFormError.tsx +15 -0
  64. package/src/auth/AuthLayout.tsx +69 -0
  65. package/src/auth/AuthMessagePanel.tsx +28 -0
  66. package/src/auth/AuthTextField.tsx +75 -0
  67. package/src/auth/AuthValidation.ts +39 -0
  68. package/src/pages/ForgotPassword.tsx +77 -56
  69. package/src/pages/Login.tsx +74 -78
  70. package/src/pages/PasswordReset.tsx +118 -124
  71. package/src/pages/Signup.tsx +117 -136
  72. package/test/authApi.test.ts +92 -0
  73. package/test/authPages.test.tsx +51 -0
  74. package/test/authValidation.test.ts +56 -0
  75. package/tsconfig.json +1 -0
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import { Box } from '@mui/material';
3
+ import { AuthLayout } from './AuthLayout';
4
+ import { AuthButton } from './AuthButton';
5
+
6
+ export interface AuthMessagePanelProps {
7
+ title: string;
8
+ body: string;
9
+ actionName: string;
10
+ actionHref: string;
11
+ }
12
+
13
+ /**
14
+ * Status states of the auth flows (link expired, reset complete, signup unavailable):
15
+ * a title, one line of body copy, and a single primary action.
16
+ */
17
+ export function AuthMessagePanel(props: AuthMessagePanelProps) {
18
+ const { title, body, actionName, actionHref } = props;
19
+ return (
20
+ <AuthLayout title={title} subtitle={body}>
21
+ <Box sx={{ mt: 1 }}>
22
+ <AuthButton type='button' href={actionHref}>
23
+ {actionName}
24
+ </AuthButton>
25
+ </Box>
26
+ </AuthLayout>
27
+ );
28
+ }
@@ -0,0 +1,75 @@
1
+ import React, { useState } from 'react';
2
+ import { Box, IconButton, InputAdornment, TextField, Typography } from '@mui/material';
3
+ import Visibility from '@mui/icons-material/Visibility';
4
+ import VisibilityOff from '@mui/icons-material/VisibilityOff';
5
+
6
+ export interface AuthTextFieldProps {
7
+ label: string;
8
+ value: string;
9
+ onChange: (value: string) => void;
10
+ /** Renders a reveal toggle and masks input until toggled. */
11
+ password?: boolean;
12
+ /** HTML autocomplete token (e.g. 'email', 'current-password', 'new-password'). */
13
+ autoComplete?: string;
14
+ /** HTML input type when not a password field (e.g. 'email'). */
15
+ type?: string;
16
+ autoFocus?: boolean;
17
+ disabled?: boolean;
18
+ }
19
+
20
+ /**
21
+ * Auth-surface text input: a small label above a rounded field (the label-above pattern —
22
+ * no floating-label notch fighting the corner radius). 48px tall for a comfortable touch
23
+ * target; 1rem input text so mobile browsers don't auto-zoom the field on focus.
24
+ */
25
+ export function AuthTextField(props: AuthTextFieldProps) {
26
+ const { label, value, onChange, password, autoComplete, type, autoFocus, disabled } = props;
27
+ const [revealed, setRevealed] = useState(false);
28
+
29
+ return (
30
+ <Box sx={{ mb: 2.5 }}>
31
+ <Typography
32
+ component='label'
33
+ htmlFor={`auth-field-${label}`}
34
+ sx={{ display: 'block', fontSize: '0.875rem', fontWeight: 500, color: 'text.primary', mb: 0.75 }}
35
+ >
36
+ {label}
37
+ </Typography>
38
+ <TextField
39
+ id={`auth-field-${label}`}
40
+ fullWidth
41
+ value={value}
42
+ onChange={(event) => onChange(event.target.value)}
43
+ type={password && !revealed ? 'password' : password ? 'text' : type || 'text'}
44
+ autoComplete={autoComplete}
45
+ autoFocus={autoFocus}
46
+ disabled={disabled}
47
+ InputProps={{
48
+ endAdornment: password ? (
49
+ <InputAdornment position='end'>
50
+ <IconButton
51
+ aria-label={revealed ? 'Hide password' : 'Show password'}
52
+ onClick={() => setRevealed(!revealed)}
53
+ edge='end'
54
+ sx={{ color: 'text.secondary', p: '12px' }}
55
+ >
56
+ {revealed ? <VisibilityOff sx={{ fontSize: 20 }} /> : <Visibility sx={{ fontSize: 20 }} />}
57
+ </IconButton>
58
+ </InputAdornment>
59
+ ) : undefined,
60
+ }}
61
+ sx={{
62
+ '& .MuiOutlinedInput-root': {
63
+ borderRadius: '12px',
64
+ backgroundColor: 'background.paper',
65
+ fontSize: '1rem',
66
+ '& input': { py: '12.5px', px: '14px' },
67
+ '& fieldset': { borderColor: 'divider' },
68
+ '&:hover fieldset': { borderColor: 'text.disabled' },
69
+ '&.Mui-focused fieldset': { borderColor: 'primary.main', borderWidth: '1.5px' },
70
+ },
71
+ }}
72
+ />
73
+ </Box>
74
+ );
75
+ }
@@ -0,0 +1,39 @@
1
+ import { emailRegex } from '@proteinjs/util';
2
+
3
+ /**
4
+ * Client-side validation for the auth flows. Each method returns a user-readable
5
+ * error message, or undefined when the input is valid.
6
+ */
7
+ export class AuthValidation {
8
+ /** @param invited invited users don't enter an email (the invite token carries it) */
9
+ static signup(
10
+ fields: { name: string; email: string; password: string; confirmPassword: string },
11
+ invited: boolean
12
+ ): string | undefined {
13
+ if (!fields.name.trim()) {
14
+ return 'Please enter your name.';
15
+ }
16
+
17
+ if (!invited && !emailRegex.test(fields.email.trim())) {
18
+ return 'Please enter a valid email address.';
19
+ }
20
+
21
+ if (!fields.password) {
22
+ return 'Please enter a password.';
23
+ }
24
+
25
+ if (fields.password !== fields.confirmPassword) {
26
+ return 'Passwords do not match.';
27
+ }
28
+ }
29
+
30
+ static passwordReset(fields: { newPassword: string; confirmPassword: string }): string | undefined {
31
+ if (!fields.newPassword) {
32
+ return 'Please enter a new password.';
33
+ }
34
+
35
+ if (fields.newPassword !== fields.confirmPassword) {
36
+ return 'Passwords do not match.';
37
+ }
38
+ }
39
+ }
@@ -1,6 +1,79 @@
1
- import React from 'react';
2
- import { Page, Form, Fields, textField, FormButtons, FormPage } from '@proteinjs/ui';
3
- import { routes, uiRoutes } from '@proteinjs/user';
1
+ import React, { useState } from 'react';
2
+ import { Box, Link } from '@mui/material';
3
+ import { Page } from '@proteinjs/ui';
4
+ import { uiRoutes } from '@proteinjs/user';
5
+ import { Helmet } from 'react-helmet';
6
+ import { AuthLayout } from '../auth/AuthLayout';
7
+ import { AuthTextField } from '../auth/AuthTextField';
8
+ import { AuthButton } from '../auth/AuthButton';
9
+ import { AuthFormError } from '../auth/AuthFormError';
10
+ import { AuthMessagePanel } from '../auth/AuthMessagePanel';
11
+ import { AuthApi } from '../auth/AuthApi';
12
+
13
+ const ForgotPasswordComponent: React.FC = () => {
14
+ const [email, setEmail] = useState('');
15
+ const [error, setError] = useState<string | undefined>();
16
+ const [busy, setBusy] = useState(false);
17
+ const [sent, setSent] = useState(false);
18
+
19
+ const onSubmit = async (event: React.FormEvent) => {
20
+ event.preventDefault();
21
+ setError(undefined);
22
+ setBusy(true);
23
+ try {
24
+ await new AuthApi().initiatePasswordReset(email);
25
+ setSent(true);
26
+ } catch (error: any) {
27
+ setError(error.message);
28
+ setBusy(false);
29
+ }
30
+ };
31
+
32
+ if (sent) {
33
+ return (
34
+ <AuthMessagePanel
35
+ title='Check your email'
36
+ body='We sent an email with a link to reset your password.'
37
+ actionName='Back to log in'
38
+ actionHref={`/${uiRoutes.auth.login}`}
39
+ />
40
+ );
41
+ }
42
+
43
+ return (
44
+ <>
45
+ <Helmet>
46
+ <title>Reset your password</title>
47
+ </Helmet>
48
+ <AuthLayout
49
+ title='Reset your password'
50
+ subtitle={`Enter your email and we'll send you a link to reset your password.`}
51
+ >
52
+ <form onSubmit={onSubmit} noValidate>
53
+ <AuthTextField
54
+ label='Email'
55
+ value={email}
56
+ onChange={setEmail}
57
+ type='email'
58
+ autoComplete='email'
59
+ disabled={busy}
60
+ />
61
+ <AuthFormError message={error} />
62
+ <AuthButton busy={busy}>Send reset link</AuthButton>
63
+ </form>
64
+ <Box sx={{ display: 'flex', justifyContent: 'center', mt: 3 }}>
65
+ <Link
66
+ href={`/${uiRoutes.auth.login}`}
67
+ underline='hover'
68
+ sx={{ fontSize: '0.875rem', color: 'text.secondary', py: '13px', px: 2 }}
69
+ >
70
+ Back to log in
71
+ </Link>
72
+ </Box>
73
+ </AuthLayout>
74
+ </>
75
+ );
76
+ };
4
77
 
5
78
  export const forgotPasswordPage: Page = {
6
79
  name: 'Forgot Password',
@@ -8,57 +81,5 @@ export const forgotPasswordPage: Page = {
8
81
  auth: {
9
82
  public: true,
10
83
  },
11
- component: () => (
12
- <FormPage>
13
- <Form<ForgotPasswordFields, typeof buttons>
14
- name='Forgot Password'
15
- createFields={() => new ForgotPasswordFields()}
16
- fieldLayout={['email']}
17
- buttons={buttons}
18
- />
19
- </FormPage>
20
- ),
21
- };
22
-
23
- class ForgotPasswordFields extends Fields {
24
- static create() {
25
- return new ForgotPasswordFields();
26
- }
27
-
28
- email = textField<ForgotPasswordFields>({
29
- name: 'email',
30
- });
31
- }
32
-
33
- const buttons: FormButtons<ForgotPasswordFields> = {
34
- submit: {
35
- name: 'Submit',
36
- style: {
37
- color: 'primary',
38
- variant: 'contained',
39
- },
40
- onClick: async (fields: ForgotPasswordFields, buttons: FormButtons<ForgotPasswordFields>) => {
41
- const response = await fetch(routes.initiatePasswordReset.path, {
42
- method: routes.initiatePasswordReset.method,
43
- body: JSON.stringify({
44
- email: fields.email.field.value,
45
- }),
46
- redirect: 'follow',
47
- credentials: 'same-origin',
48
- headers: {
49
- 'Content-Type': 'application/json',
50
- },
51
- });
52
- if (response.status != 200) {
53
- throw new Error(`Failed to initiate forgot password.`);
54
- }
55
-
56
- const body = await response.json();
57
- if (body.error) {
58
- throw new Error(`Failed to initiate forgot password.`);
59
- }
60
-
61
- return `Successfully sent an email with a link to reset your password.`;
62
- },
63
- },
84
+ component: ForgotPasswordComponent,
64
85
  };
@@ -1,88 +1,84 @@
1
- import React from 'react';
2
- import { Page, Form, Fields, textField, FormButtons, clearButton, FormPage } from '@proteinjs/ui';
3
- import { routes, uiRoutes } from '@proteinjs/user';
1
+ import React, { useState } from 'react';
2
+ import { Box, Link } from '@mui/material';
3
+ import { Page } from '@proteinjs/ui';
4
+ import { uiRoutes } from '@proteinjs/user';
4
5
  import { Helmet } from 'react-helmet';
6
+ import { AuthLayout } from '../auth/AuthLayout';
7
+ import { AuthTextField } from '../auth/AuthTextField';
8
+ import { AuthButton } from '../auth/AuthButton';
9
+ import { AuthFormError } from '../auth/AuthFormError';
10
+ import { AuthApi } from '../auth/AuthApi';
5
11
 
6
- export const loginPage: Page = {
7
- name: 'Login',
8
- path: uiRoutes.auth.login,
9
- auth: {
10
- public: true,
11
- },
12
- component: () => (
12
+ const LoginComponent: React.FC = () => {
13
+ const [email, setEmail] = useState('');
14
+ const [password, setPassword] = useState('');
15
+ const [error, setError] = useState<string | undefined>();
16
+ const [busy, setBusy] = useState(false);
17
+
18
+ const onSubmit = async (event: React.FormEvent) => {
19
+ event.preventDefault();
20
+ setError(undefined);
21
+ setBusy(true);
22
+ try {
23
+ await new AuthApi().login(email, password);
24
+ window.location.href = '/';
25
+ } catch (error: any) {
26
+ setError(error.message);
27
+ setBusy(false);
28
+ }
29
+ };
30
+
31
+ return (
13
32
  <>
14
33
  <Helmet>
15
- <title>Login</title>
34
+ <title>Log in</title>
16
35
  </Helmet>
17
- <FormPage>
18
- <Form<LoginFields, typeof buttons>
19
- name='Login'
20
- createFields={() => new LoginFields()}
21
- fieldLayout={['email', 'password']}
22
- buttons={buttons}
23
- maxWidth={'xs'}
24
- />
25
- </FormPage>
36
+ <AuthLayout title='Log in'>
37
+ <form onSubmit={onSubmit} noValidate>
38
+ <AuthTextField
39
+ label='Email'
40
+ value={email}
41
+ onChange={setEmail}
42
+ type='email'
43
+ autoComplete='email'
44
+ disabled={busy}
45
+ />
46
+ <AuthTextField
47
+ label='Password'
48
+ value={password}
49
+ onChange={setPassword}
50
+ password
51
+ autoComplete='current-password'
52
+ disabled={busy}
53
+ />
54
+ <AuthFormError message={error} />
55
+ <AuthButton busy={busy}>Log in</AuthButton>
56
+ </form>
57
+ <Box sx={{ display: 'flex', justifyContent: 'center', mt: 3 }}>
58
+ <Link
59
+ href={`/${uiRoutes.auth.forgotPassword}`}
60
+ underline='hover'
61
+ sx={{
62
+ fontSize: '0.875rem',
63
+ color: 'text.secondary',
64
+ // 44px touch target without visual bulk.
65
+ py: '13px',
66
+ px: 2,
67
+ }}
68
+ >
69
+ Forgot password?
70
+ </Link>
71
+ </Box>
72
+ </AuthLayout>
26
73
  </>
27
- ),
74
+ );
28
75
  };
29
76
 
30
- class LoginFields extends Fields {
31
- static create() {
32
- return new LoginFields();
33
- }
34
-
35
- email = textField<LoginFields>({
36
- name: 'email',
37
- });
38
-
39
- password = textField<LoginFields>({
40
- name: 'password',
41
- isPassword: true,
42
- });
43
- }
44
-
45
- const buttons: FormButtons<LoginFields> = {
46
- forgotPassword: {
47
- name: 'Forgot password',
48
- style: {
49
- variant: 'text',
50
- align: 'left',
51
- },
52
- redirect: async () => {
53
- return { path: `/${uiRoutes.auth.forgotPassword}` };
54
- },
55
- },
56
- clear: clearButton,
57
- login: {
58
- name: 'Login',
59
- style: {
60
- color: 'primary',
61
- variant: 'contained',
62
- },
63
- onClick: async (fields: LoginFields, buttons: FormButtons<LoginFields>) => {
64
- const response = await fetch(routes.login.path, {
65
- method: routes.login.method,
66
- body: JSON.stringify({
67
- email: fields.email.field.value,
68
- password: fields.password.field.value,
69
- }),
70
- redirect: 'follow',
71
- credentials: 'same-origin',
72
- headers: {
73
- 'Content-Type': 'application/json',
74
- },
75
- });
76
- if (response.status != 200) {
77
- throw new Error(`Failed to login, error: ${response.statusText}`);
78
- }
79
-
80
- const body = await response.json();
81
- if (body.error) {
82
- throw new Error(body.error);
83
- }
84
-
85
- window.location.href = '/';
86
- },
77
+ export const loginPage: Page = {
78
+ name: 'Login',
79
+ path: uiRoutes.auth.login,
80
+ auth: {
81
+ public: true,
87
82
  },
83
+ component: LoginComponent,
88
84
  };