@mblaney/holster-browser 1.1.0 → 2.0.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.
package/README.md CHANGED
@@ -14,6 +14,7 @@ Peer dependencies: `@mblaney/holster`, `@mui/material`, `@mui/icons-material`, `
14
14
  import {
15
15
  Login,
16
16
  Register,
17
+ Signup,
17
18
  RequestCode,
18
19
  ValidateEmail,
19
20
  ResetPassword,
@@ -22,7 +23,6 @@ import {
22
23
  LoginCodes,
23
24
  EditCache,
24
25
  SearchAppBar,
25
- registerServiceWorker,
26
26
  useAccountSync,
27
27
  } from "@mblaney/holster-browser"
28
28
  ```
@@ -58,14 +58,59 @@ const appBar = {
58
58
 
59
59
  #### Auth components
60
60
 
61
- `Login`, `Register`, `RequestCode`, `ValidateEmail`, `ResetPassword`, and `UpdatePassword` all accept `user`, `mode`, `setMode`, and `appBar` props. They render a `SearchAppBar` only when the user is already logged in.
61
+ `Login`, `Register`, `Signup`, `RequestCode`, `ValidateEmail`, `ResetPassword`, and `UpdatePassword` render only their own form content (fields, submit button, status message) — no page container, card, or app bar. This keeps holster-browser out of the business of page layout, so each app is free to wrap them however it likes, including its own home nav or logo.
62
+
63
+ `Register` needs the login code supplied up front (obtained via the waitlist flow or shared directly). `Signup` instead has the server assign a login code automatically from a pool (see the `signup` option in [holster-router](https://github.com/mblaney/holster-router)); the code isn't provided by the client, it's sent back in the sign up email that also asks the user to validate their address.
64
+
65
+ Each component's props:
66
+
67
+ | Component | Props |
68
+ | --- | --- |
69
+ | `Login` | `user`, `host` |
70
+ | `Register` | `user` |
71
+ | `Signup` | `user` |
72
+ | `RequestCode` | *(none)* |
73
+ | `ValidateEmail` | `code`, `validate` |
74
+ | `ResetPassword` | `loggedIn` |
75
+ | `UpdatePassword` | `user`, `loggedIn`, `current`, `code`, `reset` |
76
+
77
+ `ValidateEmail`'s `code`/`validate` and `UpdatePassword`'s `current`/`code`/`reset` come from the query params on the links in the sign up and reset-password emails respectively.
78
+
79
+ Wrap them in your own layout, e.g.:
80
+
81
+ ```jsx
82
+ const FormLayout = ({loggedIn, mode, setMode, appBar, children}) => (
83
+ <>
84
+ {loggedIn && <SearchAppBar mode={mode} setMode={setMode} {...appBar} />}
85
+ <Container maxWidth="sm">
86
+ <Grid container>
87
+ <Grid item xs={12}>
88
+ <Card sx={{mt: 2}}>
89
+ <CardContent>{children}</CardContent>
90
+ </Card>
91
+ </Grid>
92
+ </Grid>
93
+ </Container>
94
+ </>
95
+ )
96
+
97
+ <FormLayout loggedIn={!!user.is} mode={mode} setMode={setMode} appBar={appBar}>
98
+ <Login user={user} host={host} />
99
+ </FormLayout>
100
+ ```
62
101
 
63
102
  #### Settings
64
103
 
65
104
  A base settings page with name greeting, password change, and logout. Pass app-specific content as `children`, which renders between the greeting and password cards.
66
105
 
67
106
  ```jsx
68
- <Settings user={user} mode={mode} setMode={setMode} appBar={appBar} buildDate={buildDate}>
107
+ <Settings
108
+ user={user}
109
+ mode={mode}
110
+ setMode={setMode}
111
+ appBar={appBar}
112
+ buildDate={buildDate}
113
+ >
69
114
  <LoginCodes user={user} host={host} code={code} />
70
115
  {/* other app-specific settings */}
71
116
  </Settings>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mblaney/holster-browser",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "React components for holster applications",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1,13 +1,9 @@
1
1
  import {useRef, useState} from "react"
2
2
  import Box from "@mui/material/Box"
3
3
  import Button from "@mui/material/Button"
4
- import Card from "@mui/material/Card"
5
- import CardContent from "@mui/material/CardContent"
6
4
  import Checkbox from "@mui/material/Checkbox"
7
- import Container from "@mui/material/Container"
8
5
  import FormControl from "@mui/material/FormControl"
9
6
  import FormControlLabel from "@mui/material/FormControlLabel"
10
- import Grid from "@mui/material/Grid"
11
7
  import IconButton from "@mui/material/IconButton"
12
8
  import InputAdornment from "@mui/material/InputAdornment"
13
9
  import InputLabel from "@mui/material/InputLabel"
@@ -16,15 +12,14 @@ import TextField from "@mui/material/TextField"
16
12
  import Typography from "@mui/material/Typography"
17
13
  import Visibility from "@mui/icons-material/Visibility"
18
14
  import VisibilityOff from "@mui/icons-material/VisibilityOff"
19
- import SearchAppBar from "./SearchAppBar.jsx"
20
15
 
21
- const Login = ({user, host, mode, setMode, appBar}) => {
16
+ const Login = ({user, host}) => {
22
17
  const [username, setUsername] = useState("")
23
18
  const [password, setPassword] = useState("")
24
19
  const [showPassword, setShowPassword] = useState(false)
25
20
  const [stayLoggedIn, setStayLoggedIn] = useState(false)
26
21
  const [message, setMessage] = useState(user.is ? "Already logged in" : "")
27
- const [disabledButton, setDisabledButton] = useState(user.is)
22
+ const [disabledButton, setDisabledButton] = useState(!!user.is)
28
23
  const found = useRef(false)
29
24
 
30
25
  const checkAccount = async () => {
@@ -111,79 +106,60 @@ const Login = ({user, host, mode, setMode, appBar}) => {
111
106
 
112
107
  return (
113
108
  <>
114
- {user.is && (
115
- <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
116
- )}
117
- <Container maxWidth="sm">
118
- <Grid container>
119
- <Grid item xs={12}>
120
- <Card sx={{mt: 2}}>
121
- <CardContent>
122
- <Typography variant="h5">Login</Typography>
123
- <TextField
124
- id="login-username"
125
- label="Username"
126
- variant="outlined"
127
- fullWidth={true}
128
- margin="normal"
129
- value={username}
130
- onChange={event => setUsername(event.target.value)}
131
- InputLabelProps={{shrink: true}}
132
- />
133
- <FormControl
134
- variant="outlined"
135
- fullWidth={true}
136
- margin="normal"
137
- value={password}
138
- onChange={event => setPassword(event.target.value)}
139
- >
140
- <InputLabel htmlFor="login-password">Password</InputLabel>
141
- <OutlinedInput
142
- id="login-password"
143
- type={showPassword ? "text" : "password"}
144
- endAdornment={
145
- <InputAdornment position="end">
146
- <IconButton
147
- aria-label="toggle password visibility"
148
- onClick={() => setShowPassword(show => !show)}
149
- edge="end"
150
- >
151
- {showPassword ? <VisibilityOff /> : <Visibility />}
152
- </IconButton>
153
- </InputAdornment>
154
- }
155
- label="Password"
156
- />
157
- </FormControl>
158
- <Box sx={{display: "flex", alignItems: "center", mt: 1}}>
159
- {!user.is && !message && (
160
- <FormControlLabel
161
- control={
162
- <Checkbox
163
- checked={stayLoggedIn}
164
- onChange={e => setStayLoggedIn(e.target.checked)}
165
- />
166
- }
167
- label="Stay logged in"
168
- />
169
- )}
170
- {message && (
171
- <Typography variant="body2">{message}</Typography>
172
- )}
173
- <Button
174
- sx={{ml: "auto"}}
175
- variant="contained"
176
- disabled={disabledButton}
177
- onClick={() => login(username)}
178
- >
179
- Submit
180
- </Button>
181
- </Box>
182
- </CardContent>
183
- </Card>
184
- </Grid>
185
- </Grid>
186
- </Container>
109
+ <Typography variant="h5">Login</Typography>
110
+ <TextField
111
+ id="login-username"
112
+ label="Username"
113
+ variant="outlined"
114
+ fullWidth={true}
115
+ margin="normal"
116
+ value={username}
117
+ onChange={event => setUsername(event.target.value)}
118
+ slotProps={{inputLabel: {shrink: true}}}
119
+ />
120
+ <FormControl variant="outlined" fullWidth={true} margin="normal">
121
+ <InputLabel htmlFor="login-password">Password</InputLabel>
122
+ <OutlinedInput
123
+ id="login-password"
124
+ type={showPassword ? "text" : "password"}
125
+ value={password}
126
+ onChange={event => setPassword(event.target.value)}
127
+ endAdornment={
128
+ <InputAdornment position="end">
129
+ <IconButton
130
+ aria-label="toggle password visibility"
131
+ onClick={() => setShowPassword(show => !show)}
132
+ edge="end"
133
+ >
134
+ {showPassword ? <VisibilityOff /> : <Visibility />}
135
+ </IconButton>
136
+ </InputAdornment>
137
+ }
138
+ label="Password"
139
+ />
140
+ </FormControl>
141
+ <Box sx={{display: "flex", alignItems: "center", mt: 1}}>
142
+ {!user.is && !message && (
143
+ <FormControlLabel
144
+ control={
145
+ <Checkbox
146
+ checked={stayLoggedIn}
147
+ onChange={e => setStayLoggedIn(e.target.checked)}
148
+ />
149
+ }
150
+ label="Stay logged in"
151
+ />
152
+ )}
153
+ {message && <Typography variant="body2">{message}</Typography>}
154
+ <Button
155
+ sx={{ml: "auto"}}
156
+ variant="contained"
157
+ disabled={disabledButton}
158
+ onClick={() => login(username)}
159
+ >
160
+ Submit
161
+ </Button>
162
+ </Box>
187
163
  </>
188
164
  )
189
165
  }
@@ -43,11 +43,7 @@ const LoginCodes = ({user, host, code}) => {
43
43
  }
44
44
 
45
45
  secret = await user.SEA.secret({epub: epub}, user.is)
46
- user
47
- .get([host, "shared"])
48
- .next("login_codes")
49
- .next(code)
50
- .on(update, true)
46
+ user.get([host, "shared"]).next("login_codes").next(code).on(update, true)
51
47
  })
52
48
 
53
49
  return () => {
@@ -1,10 +1,6 @@
1
1
  import {useState} from "react"
2
2
  import Button from "@mui/material/Button"
3
- import Card from "@mui/material/Card"
4
- import CardContent from "@mui/material/CardContent"
5
- import Container from "@mui/material/Container"
6
3
  import FormControl from "@mui/material/FormControl"
7
- import Grid from "@mui/material/Grid"
8
4
  import IconButton from "@mui/material/IconButton"
9
5
  import InputAdornment from "@mui/material/InputAdornment"
10
6
  import InputLabel from "@mui/material/InputLabel"
@@ -13,16 +9,15 @@ import TextField from "@mui/material/TextField"
13
9
  import Typography from "@mui/material/Typography"
14
10
  import Visibility from "@mui/icons-material/Visibility"
15
11
  import VisibilityOff from "@mui/icons-material/VisibilityOff"
16
- import SearchAppBar from "./SearchAppBar.jsx"
17
12
 
18
- const Register = ({user, mode, setMode, appBar}) => {
13
+ const Register = ({user}) => {
19
14
  const [username, setUsername] = useState("")
20
15
  const [password, setPassword] = useState("")
21
16
  const [showPassword, setShowPassword] = useState(false)
22
17
  const [email, setEmail] = useState("")
23
18
  const [code, setCode] = useState("")
24
19
  const [message, setMessage] = useState(user.is ? "Already logged in" : "")
25
- const [disabledButton, setDisabledButton] = useState(user.is)
20
+ const [disabledButton, setDisabledButton] = useState(!!user.is)
26
21
 
27
22
  const register = () => {
28
23
  if (!username) {
@@ -97,86 +92,69 @@ const Register = ({user, mode, setMode, appBar}) => {
97
92
 
98
93
  return (
99
94
  <>
100
- {user.is && (
101
- <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
95
+ <Typography variant="h5">Register</Typography>
96
+ <TextField
97
+ id="register-username"
98
+ label="Username"
99
+ variant="outlined"
100
+ fullWidth={true}
101
+ margin="normal"
102
+ value={username}
103
+ onChange={event => setUsername(event.target.value)}
104
+ slotProps={{inputLabel: {shrink: true}}}
105
+ />
106
+ <FormControl variant="outlined" fullWidth={true} margin="normal">
107
+ <InputLabel htmlFor="register-password">Password</InputLabel>
108
+ <OutlinedInput
109
+ id="register-password"
110
+ type={showPassword ? "text" : "password"}
111
+ value={password}
112
+ onChange={event => setPassword(event.target.value)}
113
+ endAdornment={
114
+ <InputAdornment position="end">
115
+ <IconButton
116
+ aria-label="toggle password visibility"
117
+ onClick={() => setShowPassword(show => !show)}
118
+ edge="end"
119
+ >
120
+ {showPassword ? <VisibilityOff /> : <Visibility />}
121
+ </IconButton>
122
+ </InputAdornment>
123
+ }
124
+ label="Password"
125
+ />
126
+ </FormControl>
127
+ <TextField
128
+ id="register-email"
129
+ label="Email"
130
+ variant="outlined"
131
+ fullWidth={true}
132
+ margin="normal"
133
+ value={email}
134
+ onChange={event => setEmail(event.target.value)}
135
+ />
136
+ <TextField
137
+ id="register-code"
138
+ label="Login code"
139
+ variant="outlined"
140
+ fullWidth={true}
141
+ margin="normal"
142
+ value={code}
143
+ onChange={event => setCode(event.target.value)}
144
+ />
145
+ <Button
146
+ sx={{mt: 1}}
147
+ variant="contained"
148
+ disabled={disabledButton}
149
+ onClick={register}
150
+ >
151
+ Submit
152
+ </Button>
153
+ {message && (
154
+ <Typography sx={{m: 1}} variant="string">
155
+ {message}
156
+ </Typography>
102
157
  )}
103
- <Container maxWidth="sm">
104
- <Grid container>
105
- <Grid item xs={12}>
106
- <Card sx={{mt: 2}}>
107
- <CardContent>
108
- <Typography variant="h5">Register</Typography>
109
- <TextField
110
- id="register-username"
111
- label="Username"
112
- variant="outlined"
113
- fullWidth={true}
114
- margin="normal"
115
- value={username}
116
- onChange={event => setUsername(event.target.value)}
117
- InputLabelProps={{shrink: true}}
118
- />
119
- <FormControl
120
- variant="outlined"
121
- fullWidth={true}
122
- margin="normal"
123
- value={password}
124
- onChange={event => setPassword(event.target.value)}
125
- >
126
- <InputLabel htmlFor="register-password">Password</InputLabel>
127
- <OutlinedInput
128
- id="register-password"
129
- type={showPassword ? "text" : "password"}
130
- endAdornment={
131
- <InputAdornment position="end">
132
- <IconButton
133
- aria-label="toggle password visibility"
134
- onClick={() => setShowPassword(show => !show)}
135
- edge="end"
136
- >
137
- {showPassword ? <VisibilityOff /> : <Visibility />}
138
- </IconButton>
139
- </InputAdornment>
140
- }
141
- label="Password"
142
- />
143
- </FormControl>
144
- <TextField
145
- id="register-email"
146
- label="Email"
147
- variant="outlined"
148
- fullWidth={true}
149
- margin="normal"
150
- value={email}
151
- onChange={event => setEmail(event.target.value)}
152
- />
153
- <TextField
154
- id="register-code"
155
- label="Login code"
156
- variant="outlined"
157
- fullWidth={true}
158
- margin="normal"
159
- value={code}
160
- onChange={event => setCode(event.target.value)}
161
- />
162
- <Button
163
- sx={{mt: 1}}
164
- variant="contained"
165
- disabled={disabledButton}
166
- onClick={register}
167
- >
168
- Submit
169
- </Button>
170
- {message && (
171
- <Typography sx={{m: 1}} variant="string">
172
- {message}
173
- </Typography>
174
- )}
175
- </CardContent>
176
- </Card>
177
- </Grid>
178
- </Grid>
179
- </Container>
180
158
  </>
181
159
  )
182
160
  }
@@ -1,14 +1,9 @@
1
1
  import {useState} from "react"
2
2
  import Button from "@mui/material/Button"
3
- import Card from "@mui/material/Card"
4
- import CardContent from "@mui/material/CardContent"
5
- import Container from "@mui/material/Container"
6
- import Grid from "@mui/material/Grid"
7
3
  import TextField from "@mui/material/TextField"
8
4
  import Typography from "@mui/material/Typography"
9
- import SearchAppBar from "./SearchAppBar.jsx"
10
5
 
11
- const RequestCode = ({loggedIn, mode, setMode, appBar}) => {
6
+ const RequestCode = () => {
12
7
  const [email, setEmail] = useState("")
13
8
  const [message, setMessage] = useState("")
14
9
  const [disabledButton, setDisabledButton] = useState(false)
@@ -29,6 +24,10 @@ const RequestCode = ({loggedIn, mode, setMode, appBar}) => {
29
24
  .then(res => res.text().then(text => ({ok: res.ok, text: text})))
30
25
  .then(res => {
31
26
  setDisabledButton(false)
27
+ if (!res.ok) {
28
+ setMessage(res.text)
29
+ return
30
+ }
32
31
  setEmail("")
33
32
  setMessage(res.text)
34
33
  })
@@ -36,42 +35,29 @@ const RequestCode = ({loggedIn, mode, setMode, appBar}) => {
36
35
 
37
36
  return (
38
37
  <>
39
- {loggedIn && (
40
- <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
38
+ <Typography variant="h5">Request login code</Typography>
39
+ <TextField
40
+ id="request-email"
41
+ label="Email"
42
+ variant="outlined"
43
+ fullWidth={true}
44
+ margin="normal"
45
+ value={email}
46
+ onChange={event => setEmail(event.target.value)}
47
+ />
48
+ <Button
49
+ sx={{mt: 1}}
50
+ variant="contained"
51
+ disabled={disabledButton}
52
+ onClick={request}
53
+ >
54
+ Submit
55
+ </Button>
56
+ {message && (
57
+ <Typography sx={{m: 1}} variant="string">
58
+ {message}
59
+ </Typography>
41
60
  )}
42
- <Container maxWidth="sm">
43
- <Grid container>
44
- <Grid item xs={12}>
45
- <Card sx={{mt: 2}}>
46
- <CardContent>
47
- <Typography variant="h5">Request login code</Typography>
48
- <TextField
49
- id="request-email"
50
- label="Email"
51
- variant="outlined"
52
- fullWidth={true}
53
- margin="normal"
54
- value={email}
55
- onChange={event => setEmail(event.target.value)}
56
- />
57
- <Button
58
- sx={{mt: 1}}
59
- variant="contained"
60
- disabled={disabledButton}
61
- onClick={request}
62
- >
63
- Submit
64
- </Button>
65
- {message && (
66
- <Typography sx={{m: 1}} variant="string">
67
- {message}
68
- </Typography>
69
- )}
70
- </CardContent>
71
- </Card>
72
- </Grid>
73
- </Grid>
74
- </Container>
75
61
  </>
76
62
  )
77
63
  }
@@ -1,14 +1,9 @@
1
1
  import {useState} from "react"
2
2
  import Button from "@mui/material/Button"
3
- import Card from "@mui/material/Card"
4
- import CardContent from "@mui/material/CardContent"
5
- import Container from "@mui/material/Container"
6
- import Grid from "@mui/material/Grid"
7
3
  import TextField from "@mui/material/TextField"
8
4
  import Typography from "@mui/material/Typography"
9
- import SearchAppBar from "./SearchAppBar.jsx"
10
5
 
11
- const ResetPassword = ({loggedIn, mode, setMode, appBar}) => {
6
+ const ResetPassword = ({loggedIn}) => {
12
7
  const [email, setEmail] = useState("")
13
8
  const [code, setCode] = useState("")
14
9
  const [message, setMessage] = useState(loggedIn ? "Already logged in" : "")
@@ -27,60 +22,47 @@ const ResetPassword = ({loggedIn, mode, setMode, appBar}) => {
27
22
  headers: {"Content-Type": "application/json;charset=utf-8"},
28
23
  body: JSON.stringify({email: email, code: code}),
29
24
  })
30
- .then(res => res.text())
25
+ .then(res => res.text().then(text => ({ok: res.ok, text: text})))
31
26
  .then(res => {
32
27
  setDisabledButton(false)
33
- setMessage(res)
28
+ setMessage(res.text)
34
29
  })
35
30
  }
36
31
 
37
32
  return (
38
33
  <>
39
- {loggedIn && (
40
- <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
34
+ <Typography variant="h5">Reset Password</Typography>
35
+ <TextField
36
+ id="reset-password-email"
37
+ label="Email"
38
+ variant="outlined"
39
+ fullWidth={true}
40
+ margin="normal"
41
+ value={email}
42
+ onChange={event => setEmail(event.target.value)}
43
+ />
44
+ <TextField
45
+ id="reset-password-code"
46
+ label="Login code"
47
+ variant="outlined"
48
+ fullWidth={true}
49
+ margin="normal"
50
+ value={code}
51
+ onChange={event => setCode(event.target.value)}
52
+ />
53
+ <Button
54
+ sx={{mt: 1}}
55
+ variant="contained"
56
+ disabled={disabledButton}
57
+ onClick={reset}
58
+ >
59
+ Submit
60
+ </Button>
61
+ {message && (
62
+ <Typography sx={{m: 1}} variant="string">
63
+ {message}
64
+ </Typography>
41
65
  )}
42
- <Container maxWidth="sm">
43
- <Grid container>
44
- <Grid item xs={12}>
45
- <Card sx={{mt: 2}}>
46
- <CardContent>
47
- <Typography variant="h5">Reset Password</Typography>
48
- <TextField
49
- id="reset-password-email"
50
- label="Email"
51
- variant="outlined"
52
- fullWidth={true}
53
- margin="normal"
54
- value={email}
55
- onChange={event => setEmail(event.target.value)}
56
- />
57
- <TextField
58
- id="reset-password-code"
59
- label="Login code"
60
- variant="outlined"
61
- fullWidth={true}
62
- margin="normal"
63
- value={code}
64
- onChange={event => setCode(event.target.value)}
65
- />
66
- <Button
67
- sx={{mt: 1}}
68
- variant="contained"
69
- disabled={disabledButton}
70
- onClick={reset}
71
- >
72
- Submit
73
- </Button>
74
- {message && (
75
- <Typography sx={{m: 1}} variant="string">
76
- {message}
77
- </Typography>
78
- )}
79
- </CardContent>
80
- </Card>
81
- </Grid>
82
- </Grid>
83
- </Container>
84
66
  </>
85
67
  )
86
68
  }
@@ -51,9 +51,7 @@ const Settings = ({user, mode, setMode, appBar, buildDate, children}) => {
51
51
 
52
52
  return (
53
53
  <>
54
- {user.is && (
55
- <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
56
- )}
54
+ {user.is && <SearchAppBar mode={mode} setMode={setMode} {...appBar} />}
57
55
  <Container maxWidth="sm">
58
56
  <Grid container>
59
57
  <Grid item xs={12}>
@@ -76,8 +74,6 @@ const Settings = ({user, mode, setMode, appBar, buildDate, children}) => {
76
74
  variant="outlined"
77
75
  fullWidth={true}
78
76
  margin="normal"
79
- value={password}
80
- onChange={event => setPassword(event.target.value)}
81
77
  >
82
78
  <InputLabel htmlFor="settings-password">
83
79
  Current Password
@@ -86,6 +82,8 @@ const Settings = ({user, mode, setMode, appBar, buildDate, children}) => {
86
82
  id="settings-password"
87
83
  autoComplete="password"
88
84
  type={showPassword ? "text" : "password"}
85
+ value={password}
86
+ onChange={event => setPassword(event.target.value)}
89
87
  endAdornment={
90
88
  <InputAdornment position="end">
91
89
  <IconButton
@@ -104,8 +102,6 @@ const Settings = ({user, mode, setMode, appBar, buildDate, children}) => {
104
102
  variant="outlined"
105
103
  fullWidth={true}
106
104
  margin="normal"
107
- value={newPassword}
108
- onChange={event => setNewPassword(event.target.value)}
109
105
  >
110
106
  <InputLabel htmlFor="settings-new-password">
111
107
  New Password
@@ -114,6 +110,8 @@ const Settings = ({user, mode, setMode, appBar, buildDate, children}) => {
114
110
  id="settings-new-password"
115
111
  autoComplete="new-password"
116
112
  type={showNewPassword ? "text" : "password"}
113
+ value={newPassword}
114
+ onChange={event => setNewPassword(event.target.value)}
117
115
  endAdornment={
118
116
  <InputAdornment position="end">
119
117
  <IconButton
@@ -0,0 +1,138 @@
1
+ import {useState} from "react"
2
+ import Button from "@mui/material/Button"
3
+ import FormControl from "@mui/material/FormControl"
4
+ import IconButton from "@mui/material/IconButton"
5
+ import InputAdornment from "@mui/material/InputAdornment"
6
+ import InputLabel from "@mui/material/InputLabel"
7
+ import OutlinedInput from "@mui/material/OutlinedInput"
8
+ import TextField from "@mui/material/TextField"
9
+ import Typography from "@mui/material/Typography"
10
+ import Visibility from "@mui/icons-material/Visibility"
11
+ import VisibilityOff from "@mui/icons-material/VisibilityOff"
12
+
13
+ const Signup = ({user}) => {
14
+ const [username, setUsername] = useState("")
15
+ const [password, setPassword] = useState("")
16
+ const [showPassword, setShowPassword] = useState(false)
17
+ const [email, setEmail] = useState("")
18
+ const [message, setMessage] = useState(user.is ? "Already logged in" : "")
19
+ const [disabledButton, setDisabledButton] = useState(!!user.is)
20
+
21
+ const signup = () => {
22
+ if (!username) {
23
+ setMessage("Please choose a username")
24
+ return
25
+ }
26
+ if (!/^\w+$/.test(username)) {
27
+ setMessage("Username must contain only numbers, letters and underscore")
28
+ return
29
+ }
30
+ if (!email) {
31
+ setMessage("Please provide your email")
32
+ return
33
+ }
34
+
35
+ setDisabledButton(true)
36
+ setMessage("Creating account...")
37
+
38
+ user.create(username, password, err => {
39
+ if (err) {
40
+ setDisabledButton(false)
41
+ setMessage(err)
42
+ return
43
+ }
44
+
45
+ user.auth(username, password, err => {
46
+ if (err) {
47
+ setDisabledButton(false)
48
+ setMessage(err)
49
+ return
50
+ }
51
+
52
+ fetch(`${window.location.origin}/signup`, {
53
+ method: "POST",
54
+ headers: {"Content-Type": "application/json;charset=utf-8"},
55
+ body: JSON.stringify({
56
+ pub: user.is.pub,
57
+ epub: user.is.epub,
58
+ username: username,
59
+ email: email,
60
+ }),
61
+ })
62
+ .then(res => res.text().then(text => ({ok: res.ok, text: text})))
63
+ .then(res => {
64
+ setDisabledButton(false)
65
+ if (!res.ok) {
66
+ user.delete(username, password, console.log)
67
+ setMessage(res.text)
68
+ return
69
+ }
70
+
71
+ setMessage("Account created")
72
+ window.location = "/login"
73
+ })
74
+ })
75
+ })
76
+ }
77
+
78
+ return (
79
+ <>
80
+ <Typography variant="h5">Sign up</Typography>
81
+ <TextField
82
+ id="signup-username"
83
+ label="Username"
84
+ variant="outlined"
85
+ fullWidth={true}
86
+ margin="normal"
87
+ value={username}
88
+ onChange={event => setUsername(event.target.value)}
89
+ slotProps={{inputLabel: {shrink: true}}}
90
+ />
91
+ <FormControl variant="outlined" fullWidth={true} margin="normal">
92
+ <InputLabel htmlFor="signup-password">Password</InputLabel>
93
+ <OutlinedInput
94
+ id="signup-password"
95
+ type={showPassword ? "text" : "password"}
96
+ value={password}
97
+ onChange={event => setPassword(event.target.value)}
98
+ endAdornment={
99
+ <InputAdornment position="end">
100
+ <IconButton
101
+ aria-label="toggle password visibility"
102
+ onClick={() => setShowPassword(show => !show)}
103
+ edge="end"
104
+ >
105
+ {showPassword ? <VisibilityOff /> : <Visibility />}
106
+ </IconButton>
107
+ </InputAdornment>
108
+ }
109
+ label="Password"
110
+ />
111
+ </FormControl>
112
+ <TextField
113
+ id="signup-email"
114
+ label="Email"
115
+ variant="outlined"
116
+ fullWidth={true}
117
+ margin="normal"
118
+ value={email}
119
+ onChange={event => setEmail(event.target.value)}
120
+ />
121
+ <Button
122
+ sx={{mt: 1}}
123
+ variant="contained"
124
+ disabled={disabledButton}
125
+ onClick={signup}
126
+ >
127
+ Submit
128
+ </Button>
129
+ {message && (
130
+ <Typography sx={{m: 1}} variant="string">
131
+ {message}
132
+ </Typography>
133
+ )}
134
+ </>
135
+ )
136
+ }
137
+
138
+ export default Signup
@@ -1,10 +1,6 @@
1
1
  import {useState} from "react"
2
2
  import Button from "@mui/material/Button"
3
- import Card from "@mui/material/Card"
4
- import CardContent from "@mui/material/CardContent"
5
- import Container from "@mui/material/Container"
6
3
  import FormControl from "@mui/material/FormControl"
7
- import Grid from "@mui/material/Grid"
8
4
  import IconButton from "@mui/material/IconButton"
9
5
  import InputAdornment from "@mui/material/InputAdornment"
10
6
  import InputLabel from "@mui/material/InputLabel"
@@ -13,18 +9,8 @@ import TextField from "@mui/material/TextField"
13
9
  import Typography from "@mui/material/Typography"
14
10
  import Visibility from "@mui/icons-material/Visibility"
15
11
  import VisibilityOff from "@mui/icons-material/VisibilityOff"
16
- import SearchAppBar from "./SearchAppBar.jsx"
17
12
 
18
- const UpdatePassword = ({
19
- user,
20
- loggedIn,
21
- current,
22
- code,
23
- reset,
24
- mode,
25
- setMode,
26
- appBar,
27
- }) => {
13
+ const UpdatePassword = ({user, loggedIn, current, code, reset}) => {
28
14
  const [name, setName] = useState(current ?? "")
29
15
  const [password, setPassword] = useState("")
30
16
  const [showPassword, setShowPassword] = useState(false)
@@ -109,67 +95,50 @@ const UpdatePassword = ({
109
95
 
110
96
  return (
111
97
  <>
112
- {loggedIn && (
113
- <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
98
+ <Typography variant="h5">Update Password</Typography>
99
+ <TextField
100
+ id="update-username"
101
+ label="Username"
102
+ variant="outlined"
103
+ fullWidth={true}
104
+ margin="normal"
105
+ value={name}
106
+ onChange={event => setName(event.target.value)}
107
+ />
108
+ <FormControl variant="outlined" fullWidth={true} margin="normal">
109
+ <InputLabel htmlFor="update-password">Password</InputLabel>
110
+ <OutlinedInput
111
+ id="update-password"
112
+ type={showPassword ? "text" : "password"}
113
+ value={password}
114
+ onChange={event => setPassword(event.target.value)}
115
+ endAdornment={
116
+ <InputAdornment position="end">
117
+ <IconButton
118
+ aria-label="toggle password visibility"
119
+ onClick={() => setShowPassword(show => !show)}
120
+ edge="end"
121
+ >
122
+ {showPassword ? <VisibilityOff /> : <Visibility />}
123
+ </IconButton>
124
+ </InputAdornment>
125
+ }
126
+ label="Password"
127
+ />
128
+ </FormControl>
129
+ <Button
130
+ sx={{mt: 1}}
131
+ variant="contained"
132
+ disabled={disabledButton}
133
+ onClick={() => update(name)}
134
+ >
135
+ Submit
136
+ </Button>
137
+ {message && (
138
+ <Typography sx={{m: 1}} variant="string">
139
+ {message}
140
+ </Typography>
114
141
  )}
115
- <Container maxWidth="sm">
116
- <Grid container>
117
- <Grid item xs={12}>
118
- <Card sx={{mt: 2}}>
119
- <CardContent>
120
- <Typography variant="h5">Update Password</Typography>
121
- <TextField
122
- id="update-username"
123
- label="Username"
124
- variant="outlined"
125
- fullWidth={true}
126
- margin="normal"
127
- value={name}
128
- onChange={event => setName(event.target.value)}
129
- />
130
- <FormControl
131
- variant="outlined"
132
- fullWidth={true}
133
- margin="normal"
134
- value={password}
135
- onChange={event => setPassword(event.target.value)}
136
- >
137
- <InputLabel htmlFor="update-password">Password</InputLabel>
138
- <OutlinedInput
139
- id="update-password"
140
- type={showPassword ? "text" : "password"}
141
- endAdornment={
142
- <InputAdornment position="end">
143
- <IconButton
144
- aria-label="toggle password visibility"
145
- onClick={() => setShowPassword(show => !show)}
146
- edge="end"
147
- >
148
- {showPassword ? <VisibilityOff /> : <Visibility />}
149
- </IconButton>
150
- </InputAdornment>
151
- }
152
- label="Password"
153
- />
154
- </FormControl>
155
- <Button
156
- sx={{mt: 1}}
157
- variant="contained"
158
- disabled={disabledButton}
159
- onClick={() => update(name)}
160
- >
161
- Submit
162
- </Button>
163
- {message && (
164
- <Typography sx={{m: 1}} variant="string">
165
- {message}
166
- </Typography>
167
- )}
168
- </CardContent>
169
- </Card>
170
- </Grid>
171
- </Grid>
172
- </Container>
173
142
  </>
174
143
  )
175
144
  }
@@ -1,11 +1,8 @@
1
1
  import {useState} from "react"
2
2
  import Button from "@mui/material/Button"
3
- import Container from "@mui/material/Container"
4
- import Grid from "@mui/material/Grid"
5
3
  import Typography from "@mui/material/Typography"
6
- import SearchAppBar from "./SearchAppBar.jsx"
7
4
 
8
- const ValidateEmail = ({loggedIn, mode, setMode, appBar, code, validate}) => {
5
+ const ValidateEmail = ({code, validate}) => {
9
6
  const [message, setMessage] = useState("Validate your email address:")
10
7
  const [disabledButton, setDisabledButton] = useState(false)
11
8
 
@@ -35,24 +32,15 @@ const ValidateEmail = ({loggedIn, mode, setMode, appBar, code, validate}) => {
35
32
 
36
33
  return (
37
34
  <>
38
- {loggedIn && (
39
- <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
40
- )}
41
- <Container maxWidth="sm">
42
- <Grid container>
43
- <Grid item xs={12}>
44
- <Typography sx={{m: 1}}>{message}</Typography>
45
- <Button
46
- sx={{mt: 1}}
47
- variant="contained"
48
- disabled={disabledButton}
49
- onClick={validateEmail}
50
- >
51
- Validate
52
- </Button>
53
- </Grid>
54
- </Grid>
55
- </Container>
35
+ <Typography sx={{m: 1}}>{message}</Typography>
36
+ <Button
37
+ sx={{mt: 1}}
38
+ variant="contained"
39
+ disabled={disabledButton}
40
+ onClick={validateEmail}
41
+ >
42
+ Validate
43
+ </Button>
56
44
  </>
57
45
  )
58
46
  }
@@ -0,0 +1,183 @@
1
+ import {describe, it, expect, beforeEach, vi} from "vitest"
2
+ import {render, screen, act} from "@testing-library/react"
3
+ import userEvent from "@testing-library/user-event"
4
+ import Signup from "../Signup.jsx"
5
+
6
+ function createMockUser({createErr = null, authErr = null, is = null} = {}) {
7
+ const user = {
8
+ is,
9
+ create: vi.fn((username, password, cb) => cb(createErr)),
10
+ auth: vi.fn((username, password, cb) => {
11
+ if (!authErr) user.is = {pub: "mock-pub", epub: "mock-epub"}
12
+ cb(authErr)
13
+ }),
14
+ delete: vi.fn((username, password, cb) => cb && cb(null)),
15
+ }
16
+ return user
17
+ }
18
+
19
+ function mockFetchResponse(ok, text) {
20
+ return Promise.resolve({ok, text: () => Promise.resolve(text)})
21
+ }
22
+
23
+ async function fillAndSubmit({username, email}) {
24
+ if (username !== undefined) {
25
+ await userEvent.type(screen.getByLabelText("Username"), username)
26
+ }
27
+ if (email !== undefined) {
28
+ await userEvent.type(screen.getByLabelText("Email"), email)
29
+ }
30
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
31
+ }
32
+
33
+ describe("Signup Component", () => {
34
+ beforeEach(() => {
35
+ vi.clearAllMocks()
36
+ vi.stubGlobal("fetch", vi.fn())
37
+ delete window.location
38
+ window.location = {href: ""}
39
+ })
40
+
41
+ it("renders the signup form", () => {
42
+ const user = createMockUser()
43
+ render(<Signup user={user} />)
44
+ expect(screen.getByText("Sign up")).toBeTruthy()
45
+ expect(screen.getByLabelText("Username")).toBeTruthy()
46
+ expect(screen.getByLabelText("Email")).toBeTruthy()
47
+ })
48
+
49
+ it("shows already logged in and disables submit when a user is present", () => {
50
+ const user = createMockUser({is: {pub: "existing-pub"}})
51
+ render(<Signup user={user} />)
52
+ expect(screen.getByText("Already logged in")).toBeTruthy()
53
+ expect(screen.getByRole("button", {name: "Submit"})).toBeDisabled()
54
+ })
55
+
56
+ it("requires a username", async () => {
57
+ const user = createMockUser()
58
+ render(<Signup user={user} />)
59
+ await act(async () => {
60
+ await fillAndSubmit({email: "alice@test.com"})
61
+ })
62
+ expect(screen.getByText("Please choose a username")).toBeTruthy()
63
+ expect(user.create).not.toHaveBeenCalled()
64
+ })
65
+
66
+ it("rejects a username with invalid characters", async () => {
67
+ const user = createMockUser()
68
+ render(<Signup user={user} />)
69
+ await act(async () => {
70
+ await fillAndSubmit({username: "alice!", email: "alice@test.com"})
71
+ })
72
+ expect(
73
+ screen.getByText(
74
+ "Username must contain only numbers, letters and underscore",
75
+ ),
76
+ ).toBeTruthy()
77
+ expect(user.create).not.toHaveBeenCalled()
78
+ })
79
+
80
+ it("requires an email", async () => {
81
+ const user = createMockUser()
82
+ render(<Signup user={user} />)
83
+ await act(async () => {
84
+ await fillAndSubmit({username: "alice"})
85
+ })
86
+ expect(screen.getByText("Please provide your email")).toBeTruthy()
87
+ expect(user.create).not.toHaveBeenCalled()
88
+ })
89
+
90
+ it("submits the typed password", async () => {
91
+ const user = createMockUser()
92
+ fetch.mockReturnValue(mockFetchResponse(true, ""))
93
+ render(<Signup user={user} />)
94
+
95
+ await act(async () => {
96
+ await userEvent.type(screen.getByLabelText("Username"), "alice")
97
+ await userEvent.type(screen.getByLabelText("Password"), "hunter2")
98
+ await userEvent.type(screen.getByLabelText("Email"), "alice@test.com")
99
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
100
+ })
101
+
102
+ expect(user.create).toHaveBeenCalledWith(
103
+ "alice",
104
+ "hunter2",
105
+ expect.any(Function),
106
+ )
107
+ })
108
+
109
+ it("creates the account, posts to /signup, and redirects on success", async () => {
110
+ const user = createMockUser()
111
+ fetch.mockReturnValue(mockFetchResponse(true, ""))
112
+ render(<Signup user={user} />)
113
+
114
+ await act(async () => {
115
+ await fillAndSubmit({username: "alice", email: "alice@test.com"})
116
+ })
117
+
118
+ expect(user.create).toHaveBeenCalledWith("alice", "", expect.any(Function))
119
+ expect(user.auth).toHaveBeenCalledWith("alice", "", expect.any(Function))
120
+ expect(fetch).toHaveBeenCalledWith(
121
+ expect.stringContaining("/signup"),
122
+ expect.objectContaining({
123
+ method: "POST",
124
+ body: JSON.stringify({
125
+ pub: "mock-pub",
126
+ epub: "mock-epub",
127
+ username: "alice",
128
+ email: "alice@test.com",
129
+ }),
130
+ }),
131
+ )
132
+ expect(screen.getByText("Account created")).toBeTruthy()
133
+ expect(window.location).toBe("/login")
134
+ })
135
+
136
+ it("deletes the local account and shows the error on failure", async () => {
137
+ const user = createMockUser()
138
+ fetch.mockReturnValue(
139
+ mockFetchResponse(
140
+ false,
141
+ "Signup is not available, please try again in a few minutes",
142
+ ),
143
+ )
144
+ render(<Signup user={user} />)
145
+
146
+ await act(async () => {
147
+ await fillAndSubmit({username: "alice", email: "alice@test.com"})
148
+ })
149
+
150
+ expect(user.delete).toHaveBeenCalledWith("alice", "", expect.any(Function))
151
+ expect(
152
+ screen.getByText(
153
+ "Signup is not available, please try again in a few minutes",
154
+ ),
155
+ ).toBeTruthy()
156
+ expect(window.location).toEqual({href: ""})
157
+ })
158
+
159
+ it("shows the error and stops when user.create fails", async () => {
160
+ const user = createMockUser({createErr: "username already taken"})
161
+ render(<Signup user={user} />)
162
+
163
+ await act(async () => {
164
+ await fillAndSubmit({username: "alice", email: "alice@test.com"})
165
+ })
166
+
167
+ expect(screen.getByText("username already taken")).toBeTruthy()
168
+ expect(user.auth).not.toHaveBeenCalled()
169
+ expect(fetch).not.toHaveBeenCalled()
170
+ })
171
+
172
+ it("shows the error and stops when user.auth fails", async () => {
173
+ const user = createMockUser({authErr: "wrong password"})
174
+ render(<Signup user={user} />)
175
+
176
+ await act(async () => {
177
+ await fillAndSubmit({username: "alice", email: "alice@test.com"})
178
+ })
179
+
180
+ expect(screen.getByText("wrong password")).toBeTruthy()
181
+ expect(fetch).not.toHaveBeenCalled()
182
+ })
183
+ })
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export {default as EditCache} from "./components/EditCache.jsx"
2
2
  export {default as RequestCode} from "./components/RequestCode.jsx"
3
+ export {default as Signup} from "./components/Signup.jsx"
3
4
  export {default as LoginCodes} from "./components/LoginCodes.jsx"
4
5
  export {default as Login} from "./components/Login.jsx"
5
6
  export {default as Register} from "./components/Register.jsx"
@@ -8,5 +9,4 @@ export {default as SearchAppBar} from "./components/SearchAppBar.jsx"
8
9
  export {default as Settings} from "./components/Settings.jsx"
9
10
  export {default as UpdatePassword} from "./components/UpdatePassword.jsx"
10
11
  export {default as ValidateEmail} from "./components/ValidateEmail.jsx"
11
- export {register as registerServiceWorker} from "./utils/serviceWorkerRegistration.js"
12
12
  export {useAccountSync} from "./hooks/useAccountSync.js"
@@ -1,88 +0,0 @@
1
- const isLocalhost = Boolean(
2
- window.location.hostname === "localhost" ||
3
- window.location.hostname === "[::1]" ||
4
- window.location.hostname.match(
5
- /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/,
6
- ),
7
- )
8
-
9
- export function register(config) {
10
- if (import.meta.env.PROD && "serviceWorker" in navigator) {
11
- window.addEventListener("load", () => {
12
- const swUrl = `${import.meta.env.BASE_URL}service-worker.js`
13
-
14
- if (isLocalhost) {
15
- checkValidServiceWorker(swUrl, config)
16
- } else {
17
- registerValidSW(swUrl, config)
18
- }
19
- })
20
- }
21
- }
22
-
23
- function registerValidSW(swUrl, config) {
24
- navigator.serviceWorker
25
- .register(swUrl)
26
- .then(registration => {
27
- registration.onupdatefound = () => {
28
- const installingWorker = registration.installing
29
- if (installingWorker == null) {
30
- return
31
- }
32
- installingWorker.onstatechange = () => {
33
- if (installingWorker.state === "installed") {
34
- if (navigator.serviceWorker.controller) {
35
- if (config && config.onUpdate) {
36
- config.onUpdate(registration)
37
- }
38
- } else {
39
- if (config && config.onSuccess) {
40
- config.onSuccess(registration)
41
- }
42
- }
43
- }
44
- }
45
- }
46
- })
47
- .catch(error => {
48
- console.error("Error during service worker registration:", error)
49
- })
50
- }
51
-
52
- function checkValidServiceWorker(swUrl, config) {
53
- fetch(swUrl, {
54
- headers: {"Service-Worker": "script"},
55
- })
56
- .then(response => {
57
- const contentType = response.headers.get("content-type")
58
- if (
59
- response.status === 404 ||
60
- (contentType != null && contentType.indexOf("javascript") === -1)
61
- ) {
62
- navigator.serviceWorker.ready.then(registration => {
63
- registration.unregister().then(() => {
64
- window.location.reload()
65
- })
66
- })
67
- } else {
68
- registerValidSW(swUrl, config)
69
- }
70
- })
71
- .catch(() => {
72
- console.log(
73
- "No internet connection found. App is running in offline mode.",
74
- )
75
- })
76
- }
77
-
78
- export function unregister() {
79
- if ("serviceWorker" in navigator) {
80
- navigator.serviceWorker.ready
81
- .then(registration => {
82
- registration.unregister()
83
- })
84
- .catch(error => {
85
- console.error(error.message)
86
- })
87
- }
88
- }