@mblaney/holster-browser 1.0.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 +59 -3
- package/package.json +1 -1
- package/src/components/Login.jsx +56 -80
- package/src/components/LoginCodes.jsx +1 -5
- package/src/components/Register.jsx +64 -86
- package/src/components/RequestCode.jsx +27 -41
- package/src/components/ResetPassword.jsx +34 -52
- package/src/components/Settings.jsx +5 -7
- package/src/components/Signup.jsx +138 -0
- package/src/components/UpdatePassword.jsx +44 -75
- package/src/components/ValidateEmail.jsx +10 -22
- package/src/components/__tests__/Signup.test.jsx +183 -0
- package/src/hooks/useAccountSync.js +110 -0
- package/src/index.js +2 -1
- package/src/utils/serviceWorkerRegistration.js +0 -88
|
@@ -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
|
-
|
|
113
|
-
|
|
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 = ({
|
|
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
|
-
{
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
})
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {useEffect, useState} from "react"
|
|
2
|
+
|
|
3
|
+
export function useAccountSync(holster, user, host, code) {
|
|
4
|
+
const [accountsReady, setAccountsReady] = useState(false)
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
if (!host || !user.is) return
|
|
8
|
+
|
|
9
|
+
const updateAccounts = async accounts => {
|
|
10
|
+
if (!accounts) return
|
|
11
|
+
|
|
12
|
+
setAccountsReady(true)
|
|
13
|
+
|
|
14
|
+
// Check accounts against the users list of contacts.
|
|
15
|
+
const c = await new Promise(res => {
|
|
16
|
+
user.get("public").next("contacts", res)
|
|
17
|
+
})
|
|
18
|
+
if (!c) return
|
|
19
|
+
|
|
20
|
+
for (const [accountCode, account] of Object.entries(accounts)) {
|
|
21
|
+
if (!account) continue
|
|
22
|
+
|
|
23
|
+
// Only include properties that are present in the account
|
|
24
|
+
let check = {}
|
|
25
|
+
if (account.pub !== undefined) check.pub = account.pub
|
|
26
|
+
if (account.username !== undefined) check.username = account.username
|
|
27
|
+
if (account.name !== undefined) check.name = account.name
|
|
28
|
+
if (account.ref !== undefined) check.ref = account.ref
|
|
29
|
+
if (account.host !== undefined) check.host = account.host
|
|
30
|
+
|
|
31
|
+
let found = false
|
|
32
|
+
for (const [contactCode, contact] of Object.entries(c)) {
|
|
33
|
+
if (contactCode !== accountCode) continue
|
|
34
|
+
|
|
35
|
+
found = true
|
|
36
|
+
// The account we're checking hasn't changed.
|
|
37
|
+
if (contact.pub === check.pub) break
|
|
38
|
+
|
|
39
|
+
// If the public key has changed for this contact then store their
|
|
40
|
+
// new account details and re-share encrypted data with them to
|
|
41
|
+
// help restore their account.
|
|
42
|
+
const err = await new Promise(res => {
|
|
43
|
+
user
|
|
44
|
+
.get("public")
|
|
45
|
+
.next("contacts")
|
|
46
|
+
.next(contactCode)
|
|
47
|
+
.put(check, res)
|
|
48
|
+
})
|
|
49
|
+
if (err) console.error(err)
|
|
50
|
+
|
|
51
|
+
const oldEpub = await new Promise(res => {
|
|
52
|
+
user.get([contact.pub, "epub"], res)
|
|
53
|
+
})
|
|
54
|
+
if (!oldEpub) {
|
|
55
|
+
console.error("User not found for old public key")
|
|
56
|
+
break
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const epub = await new Promise(res => {
|
|
60
|
+
user.get([check.pub, "epub"], res)
|
|
61
|
+
})
|
|
62
|
+
if (!epub) {
|
|
63
|
+
console.error("User not found for new public key")
|
|
64
|
+
break
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const shared = await new Promise(res => {
|
|
68
|
+
user.get("shared").next(contactCode, res)
|
|
69
|
+
})
|
|
70
|
+
if (!shared) break
|
|
71
|
+
|
|
72
|
+
const oldSecret = await holster.SEA.secret(oldEpub, user.is)
|
|
73
|
+
const secret = await holster.SEA.secret(epub, user.is)
|
|
74
|
+
const update = {}
|
|
75
|
+
for (const [key, oldEnc] of Object.entries(shared)) {
|
|
76
|
+
if (!key) continue
|
|
77
|
+
|
|
78
|
+
const data = await holster.SEA.decrypt(oldEnc, oldSecret)
|
|
79
|
+
const enc = await holster.SEA.encrypt(data, secret)
|
|
80
|
+
update[key] = enc
|
|
81
|
+
}
|
|
82
|
+
if (Object.keys(update).length !== 0) {
|
|
83
|
+
const err = await new Promise(res => {
|
|
84
|
+
user.get("shared").next(contactCode).put(update, res)
|
|
85
|
+
})
|
|
86
|
+
if (err) console.error(err)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Add the new contact if we referred them.
|
|
90
|
+
if (!found && account.ref === code) {
|
|
91
|
+
user
|
|
92
|
+
.get("public")
|
|
93
|
+
.next("contacts")
|
|
94
|
+
.next(accountCode)
|
|
95
|
+
.put(check, err => {
|
|
96
|
+
if (err) console.error(err)
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
user.get([host, "accounts"]).on(updateAccounts, true)
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
user.get([host, "accounts"]).off(updateAccounts)
|
|
106
|
+
}
|
|
107
|
+
}, [host, code])
|
|
108
|
+
|
|
109
|
+
return accountsReady
|
|
110
|
+
}
|
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,4 +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 {
|
|
12
|
+
export {useAccountSync} from "./hooks/useAccountSync.js"
|