@mblaney/holster-browser 1.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/LICENSE.md +55 -0
- package/README.md +79 -0
- package/package.json +48 -0
- package/src/components/EditCache.jsx +187 -0
- package/src/components/Login.jsx +191 -0
- package/src/components/LoginCodes.jsx +102 -0
- package/src/components/Register.jsx +184 -0
- package/src/components/RequestCode.jsx +79 -0
- package/src/components/ResetPassword.jsx +88 -0
- package/src/components/SearchAppBar.jsx +290 -0
- package/src/components/Settings.jsx +177 -0
- package/src/components/UpdatePassword.jsx +177 -0
- package/src/components/ValidateEmail.jsx +60 -0
- package/src/components/__tests__/EditCache.test.jsx +237 -0
- package/src/index.js +11 -0
- package/src/setupTests.js +21 -0
- package/src/utils/serviceWorkerRegistration.js +88 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Copyright (c) 2026 Malcolm Blaney
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
Copyright (c) 2026 Malcolm Blaney
|
|
24
|
+
|
|
25
|
+
This software is provided 'as-is', without any express or implied
|
|
26
|
+
warranty. In no event will the authors be held liable for any damages
|
|
27
|
+
arising from the use of this software.
|
|
28
|
+
|
|
29
|
+
Permission is granted to anyone to use this software for any purpose,
|
|
30
|
+
including commercial applications, and to alter it and redistribute it
|
|
31
|
+
freely, subject to the following restrictions:
|
|
32
|
+
|
|
33
|
+
1. The origin of this software must not be misrepresented; you must not
|
|
34
|
+
claim that you wrote the original software. If you use this software
|
|
35
|
+
in a product, an acknowledgement in the product documentation would be
|
|
36
|
+
appreciated but is not required.
|
|
37
|
+
2. Altered source versions must be plainly marked as such, and must not be
|
|
38
|
+
misrepresented as being the original software.
|
|
39
|
+
3. This notice may not be removed or altered from any source distribution.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
Copyright 2026 Malcolm Blaney
|
|
44
|
+
|
|
45
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
46
|
+
you may not use this file except in compliance with the License.
|
|
47
|
+
You may obtain a copy of the License at
|
|
48
|
+
|
|
49
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
50
|
+
|
|
51
|
+
Unless required by applicable law or agreed to in writing, software
|
|
52
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
53
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
54
|
+
See the License for the specific language governing permissions and
|
|
55
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Holster-browser provides shared React components for [Holster](https://github.com/mblaney/holster) applications. It handles authentication UI, settings, and the app bar, so that apps built on Holster don't need to reimplement this infrastructure.
|
|
2
|
+
|
|
3
|
+
### Install
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm install @mblaney/holster-browser
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Peer dependencies: `@mblaney/holster`, `@mui/material`, `@mui/icons-material`, `@emotion/react`, `@emotion/styled`, and `react`.
|
|
10
|
+
|
|
11
|
+
### Usage
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
import {
|
|
15
|
+
Login,
|
|
16
|
+
Register,
|
|
17
|
+
RequestCode,
|
|
18
|
+
ValidateEmail,
|
|
19
|
+
ResetPassword,
|
|
20
|
+
UpdatePassword,
|
|
21
|
+
Settings,
|
|
22
|
+
LoginCodes,
|
|
23
|
+
EditCache,
|
|
24
|
+
SearchAppBar,
|
|
25
|
+
registerServiceWorker,
|
|
26
|
+
} from "@mblaney/holster-browser"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Components are designed to work with [holster-router](https://github.com/mblaney/holster-router) on the server side.
|
|
30
|
+
|
|
31
|
+
#### SearchAppBar
|
|
32
|
+
|
|
33
|
+
The app bar used across all pages. Pass an `appBar` identity object with `name`, `icon`, `iconSx`, and `menuItems` to configure the app's appearance and navigation.
|
|
34
|
+
|
|
35
|
+
```jsx
|
|
36
|
+
const appBar = {
|
|
37
|
+
name: "myapp",
|
|
38
|
+
icon: MyIcon,
|
|
39
|
+
iconSx: theme => ({...theme.applyStyles("dark", {color: "red"})}),
|
|
40
|
+
menuItems: [
|
|
41
|
+
{label: "Home", onClick: () => (window.location = "/")},
|
|
42
|
+
{label: "Settings", onClick: () => (window.location = "/settings")},
|
|
43
|
+
],
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
<SearchAppBar
|
|
47
|
+
{...appBar}
|
|
48
|
+
mode={mode}
|
|
49
|
+
setMode={setMode}
|
|
50
|
+
title="Current group"
|
|
51
|
+
onTitleClick={handleTitleClick}
|
|
52
|
+
onHomeClick={handleHomeClick}
|
|
53
|
+
onSearch={handleSearch}
|
|
54
|
+
searchQuery={searchQuery}
|
|
55
|
+
/>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### Auth components
|
|
59
|
+
|
|
60
|
+
`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
|
+
|
|
62
|
+
#### Settings
|
|
63
|
+
|
|
64
|
+
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.
|
|
65
|
+
|
|
66
|
+
```jsx
|
|
67
|
+
<Settings user={user} mode={mode} setMode={setMode} appBar={appBar} buildDate={buildDate}>
|
|
68
|
+
<LoginCodes user={user} host={host} code={code} />
|
|
69
|
+
{/* other app-specific settings */}
|
|
70
|
+
</Settings>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### LoginCodes
|
|
74
|
+
|
|
75
|
+
Displays available login codes for sharing with new users. Returns null when no codes are available.
|
|
76
|
+
|
|
77
|
+
#### EditCache
|
|
78
|
+
|
|
79
|
+
Displays audio and video cache contents with options to remove individual items or clear a cache entirely.
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mblaney/holster-browser",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React components for holster applications",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/mblaney/holster-browser.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/mblaney/holster-browser/issues"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"author": "Malcolm Blaney",
|
|
22
|
+
"license": "Zlib, MIT, Apache-2.0",
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@emotion/react": ">=11.0.0",
|
|
25
|
+
"@emotion/styled": ">=11.0.0",
|
|
26
|
+
"@mblaney/holster": ">=2.1.0",
|
|
27
|
+
"@mui/icons-material": ">=5.0.0",
|
|
28
|
+
"@mui/material": ">=5.0.0",
|
|
29
|
+
"react": ">=18.0.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "vitest --run"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@testing-library/jest-dom": "^5.17.0",
|
|
36
|
+
"@testing-library/react": "^14.0.0",
|
|
37
|
+
"@testing-library/user-event": "^13.5.0",
|
|
38
|
+
"@vitejs/plugin-react": "^4.3.1",
|
|
39
|
+
"jsdom": "^22.0.0",
|
|
40
|
+
"prettier": "^3.4.2",
|
|
41
|
+
"vitest": "^4.1.5"
|
|
42
|
+
},
|
|
43
|
+
"prettier": {
|
|
44
|
+
"semi": false,
|
|
45
|
+
"bracketSpacing": false,
|
|
46
|
+
"arrowParens": "avoid"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {useEffect, useRef, useState} from "react"
|
|
2
|
+
import Accordion from "@mui/material/Accordion"
|
|
3
|
+
import AccordionDetails from "@mui/material/AccordionDetails"
|
|
4
|
+
import AccordionSummary from "@mui/material/AccordionSummary"
|
|
5
|
+
import Box from "@mui/material/Box"
|
|
6
|
+
import Button from "@mui/material/Button"
|
|
7
|
+
import Card from "@mui/material/Card"
|
|
8
|
+
import CardContent from "@mui/material/CardContent"
|
|
9
|
+
import IconButton from "@mui/material/IconButton"
|
|
10
|
+
import Typography from "@mui/material/Typography"
|
|
11
|
+
import Delete from "@mui/icons-material/Delete"
|
|
12
|
+
import ExpandMore from "@mui/icons-material/ExpandMore"
|
|
13
|
+
|
|
14
|
+
const formatSize = bytes => {
|
|
15
|
+
if (!bytes) return "—"
|
|
16
|
+
if (bytes < 1024) return `${bytes} B`
|
|
17
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
|
18
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const displayUrl = url => {
|
|
22
|
+
try {
|
|
23
|
+
const parsed = new URL(url)
|
|
24
|
+
const file =
|
|
25
|
+
parsed.pathname.split("/").filter(Boolean).pop() || parsed.pathname
|
|
26
|
+
return `${parsed.hostname}: ${file}`
|
|
27
|
+
} catch {
|
|
28
|
+
return url
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const CacheSection = ({label, cacheName, data, removeItem, clearCache}) => {
|
|
33
|
+
const [expanded, setExpanded] = useState(false)
|
|
34
|
+
const [visibleCount, setVisibleCount] = useState(10)
|
|
35
|
+
const [itemSizes, setItemSizes] = useState({})
|
|
36
|
+
const loadedUrlsRef = useRef(new Set())
|
|
37
|
+
|
|
38
|
+
const visible = data.items.slice(0, visibleCount)
|
|
39
|
+
const hasMore = data.count > visibleCount
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!expanded || !("caches" in window)) return
|
|
43
|
+
|
|
44
|
+
const toLoad = data.items
|
|
45
|
+
.slice(0, visibleCount)
|
|
46
|
+
.filter(item => item.size === 0 && !loadedUrlsRef.current.has(item.url))
|
|
47
|
+
|
|
48
|
+
if (toLoad.length === 0) return
|
|
49
|
+
|
|
50
|
+
toLoad.forEach(item => loadedUrlsRef.current.add(item.url))
|
|
51
|
+
|
|
52
|
+
caches.open(cacheName).then(async cache => {
|
|
53
|
+
const updates = {}
|
|
54
|
+
for (const {url} of toLoad) {
|
|
55
|
+
const response = await cache.match(url)
|
|
56
|
+
if (response) {
|
|
57
|
+
const blob = await response.blob()
|
|
58
|
+
updates[url] = blob.size
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (Object.keys(updates).length > 0) {
|
|
62
|
+
setItemSizes(prev => ({...prev, ...updates}))
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
}, [expanded, visibleCount, data.items, cacheName])
|
|
66
|
+
|
|
67
|
+
const totalSize = data.items.reduce(
|
|
68
|
+
(sum, item) => sum + (itemSizes[item.url] ?? item.size),
|
|
69
|
+
0,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<Accordion
|
|
74
|
+
disableGutters
|
|
75
|
+
expanded={expanded}
|
|
76
|
+
onChange={(_, isExpanded) => setExpanded(isExpanded)}
|
|
77
|
+
>
|
|
78
|
+
<AccordionSummary expandIcon={<ExpandMore />}>
|
|
79
|
+
<Typography>
|
|
80
|
+
{label}{" "}
|
|
81
|
+
<Typography component="span" variant="body2" color="text.secondary">
|
|
82
|
+
({data.count} file{data.count !== 1 ? "s" : ""}
|
|
83
|
+
{totalSize > 0 ? `, ${formatSize(totalSize)}` : ""})
|
|
84
|
+
</Typography>
|
|
85
|
+
</Typography>
|
|
86
|
+
</AccordionSummary>
|
|
87
|
+
<AccordionDetails>
|
|
88
|
+
{data.count === 0 ? (
|
|
89
|
+
<Typography variant="body2" color="text.secondary">
|
|
90
|
+
No {label.toLowerCase()} cached
|
|
91
|
+
</Typography>
|
|
92
|
+
) : (
|
|
93
|
+
<>
|
|
94
|
+
{visible.map(item => {
|
|
95
|
+
const size = itemSizes[item.url] ?? item.size
|
|
96
|
+
return (
|
|
97
|
+
<Box
|
|
98
|
+
key={item.url}
|
|
99
|
+
sx={{display: "flex", alignItems: "center", mb: 0.5}}
|
|
100
|
+
>
|
|
101
|
+
<Typography
|
|
102
|
+
variant="body2"
|
|
103
|
+
sx={{
|
|
104
|
+
flexGrow: 1,
|
|
105
|
+
minWidth: 0,
|
|
106
|
+
overflow: "hidden",
|
|
107
|
+
textOverflow: "ellipsis",
|
|
108
|
+
whiteSpace: "nowrap",
|
|
109
|
+
}}
|
|
110
|
+
>
|
|
111
|
+
{item.name || displayUrl(item.url)}
|
|
112
|
+
</Typography>
|
|
113
|
+
<Typography
|
|
114
|
+
variant="body2"
|
|
115
|
+
color="text.secondary"
|
|
116
|
+
sx={{mx: 1, whiteSpace: "nowrap", flexShrink: 0}}
|
|
117
|
+
>
|
|
118
|
+
{formatSize(size)}
|
|
119
|
+
</Typography>
|
|
120
|
+
<IconButton
|
|
121
|
+
size="small"
|
|
122
|
+
aria-label="remove"
|
|
123
|
+
onClick={() => removeItem(cacheName, item.url)}
|
|
124
|
+
>
|
|
125
|
+
<Delete fontSize="small" />
|
|
126
|
+
</IconButton>
|
|
127
|
+
</Box>
|
|
128
|
+
)
|
|
129
|
+
})}
|
|
130
|
+
{hasMore && (
|
|
131
|
+
<Box sx={{display: "flex", alignItems: "center", mt: 0.5}}>
|
|
132
|
+
<Typography variant="body2" color="text.secondary">
|
|
133
|
+
Showing {visibleCount} of {data.count} files
|
|
134
|
+
</Typography>
|
|
135
|
+
<Button
|
|
136
|
+
size="small"
|
|
137
|
+
sx={{ml: 1}}
|
|
138
|
+
onClick={() => setVisibleCount(c => c + 10)}
|
|
139
|
+
>
|
|
140
|
+
Show more
|
|
141
|
+
</Button>
|
|
142
|
+
</Box>
|
|
143
|
+
)}
|
|
144
|
+
<Button
|
|
145
|
+
size="small"
|
|
146
|
+
color="error"
|
|
147
|
+
sx={{mt: 1}}
|
|
148
|
+
onClick={() => clearCache(cacheName)}
|
|
149
|
+
>
|
|
150
|
+
Clear all {label.toLowerCase()}
|
|
151
|
+
</Button>
|
|
152
|
+
</>
|
|
153
|
+
)}
|
|
154
|
+
</AccordionDetails>
|
|
155
|
+
</Accordion>
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const EditCache = ({cacheData, removeItem, clearCache}) => {
|
|
160
|
+
if (!cacheData) return null
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<Card sx={{mt: 2}}>
|
|
164
|
+
<CardContent>
|
|
165
|
+
<Typography variant="h6" sx={{mb: 1}}>
|
|
166
|
+
Cache storage
|
|
167
|
+
</Typography>
|
|
168
|
+
<CacheSection
|
|
169
|
+
label="Audio"
|
|
170
|
+
cacheName="audio"
|
|
171
|
+
data={cacheData.audio}
|
|
172
|
+
removeItem={removeItem}
|
|
173
|
+
clearCache={clearCache}
|
|
174
|
+
/>
|
|
175
|
+
<CacheSection
|
|
176
|
+
label="Video"
|
|
177
|
+
cacheName="video"
|
|
178
|
+
data={cacheData.video}
|
|
179
|
+
removeItem={removeItem}
|
|
180
|
+
clearCache={clearCache}
|
|
181
|
+
/>
|
|
182
|
+
</CardContent>
|
|
183
|
+
</Card>
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export default EditCache
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import {useRef, useState} from "react"
|
|
2
|
+
import Box from "@mui/material/Box"
|
|
3
|
+
import Button from "@mui/material/Button"
|
|
4
|
+
import Card from "@mui/material/Card"
|
|
5
|
+
import CardContent from "@mui/material/CardContent"
|
|
6
|
+
import Checkbox from "@mui/material/Checkbox"
|
|
7
|
+
import Container from "@mui/material/Container"
|
|
8
|
+
import FormControl from "@mui/material/FormControl"
|
|
9
|
+
import FormControlLabel from "@mui/material/FormControlLabel"
|
|
10
|
+
import Grid from "@mui/material/Grid"
|
|
11
|
+
import IconButton from "@mui/material/IconButton"
|
|
12
|
+
import InputAdornment from "@mui/material/InputAdornment"
|
|
13
|
+
import InputLabel from "@mui/material/InputLabel"
|
|
14
|
+
import OutlinedInput from "@mui/material/OutlinedInput"
|
|
15
|
+
import TextField from "@mui/material/TextField"
|
|
16
|
+
import Typography from "@mui/material/Typography"
|
|
17
|
+
import Visibility from "@mui/icons-material/Visibility"
|
|
18
|
+
import VisibilityOff from "@mui/icons-material/VisibilityOff"
|
|
19
|
+
import SearchAppBar from "./SearchAppBar.jsx"
|
|
20
|
+
|
|
21
|
+
const Login = ({user, host, mode, setMode, appBar}) => {
|
|
22
|
+
const [username, setUsername] = useState("")
|
|
23
|
+
const [password, setPassword] = useState("")
|
|
24
|
+
const [showPassword, setShowPassword] = useState(false)
|
|
25
|
+
const [stayLoggedIn, setStayLoggedIn] = useState(false)
|
|
26
|
+
const [message, setMessage] = useState(user.is ? "Already logged in" : "")
|
|
27
|
+
const [disabledButton, setDisabledButton] = useState(user.is)
|
|
28
|
+
const found = useRef(false)
|
|
29
|
+
|
|
30
|
+
const checkAccount = async () => {
|
|
31
|
+
if (!user.is) return
|
|
32
|
+
|
|
33
|
+
setMessage("Credentials verified, looking up account...")
|
|
34
|
+
const code = await new Promise(res => {
|
|
35
|
+
user.get([host, "map"]).next("account:" + user.is.pub, res)
|
|
36
|
+
})
|
|
37
|
+
if (!code) return
|
|
38
|
+
|
|
39
|
+
setMessage("Account found, verifying details...")
|
|
40
|
+
const account = await new Promise(res => {
|
|
41
|
+
user.get([host, "accounts"]).next(code, res)
|
|
42
|
+
})
|
|
43
|
+
if (!account) return
|
|
44
|
+
|
|
45
|
+
if (account.pub !== user.is.pub) {
|
|
46
|
+
// Previous account can be returned if user remembers their old password.
|
|
47
|
+
return "Wrong username or password"
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
found.current = true
|
|
51
|
+
sessionStorage.setItem("code", code)
|
|
52
|
+
sessionStorage.setItem("name", account.name)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const login = u => {
|
|
56
|
+
if (!user || !host) {
|
|
57
|
+
setMessage("Host not available")
|
|
58
|
+
localStorage.removeItem("host")
|
|
59
|
+
setTimeout(() => window.location.reload(), 1000)
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
setDisabledButton(true)
|
|
64
|
+
setMessage("Verifying credentials...")
|
|
65
|
+
user.auth(u, password, async err => {
|
|
66
|
+
if (!err) {
|
|
67
|
+
err = await checkAccount()
|
|
68
|
+
if (found.current) {
|
|
69
|
+
user.store(stayLoggedIn)
|
|
70
|
+
if (stayLoggedIn) {
|
|
71
|
+
localStorage.setItem("code", sessionStorage.getItem("code"))
|
|
72
|
+
localStorage.setItem("name", sessionStorage.getItem("name"))
|
|
73
|
+
}
|
|
74
|
+
window.location = "/"
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!err) {
|
|
79
|
+
setDisabledButton(false)
|
|
80
|
+
setMessage("Account not found. Please try logging in again")
|
|
81
|
+
window.indexedDB.deleteDatabase("radata")
|
|
82
|
+
localStorage.removeItem("host")
|
|
83
|
+
setTimeout(() => window.location.reload(), 2000)
|
|
84
|
+
user.leave()
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (err === "Wrong username or password") {
|
|
90
|
+
let match = u.match(/(.*)\.(\d)$/)
|
|
91
|
+
if (match) {
|
|
92
|
+
let increment = Number(match[2]) + 1
|
|
93
|
+
if (increment === 10) {
|
|
94
|
+
setDisabledButton(false)
|
|
95
|
+
setMessage("Wrong username or password")
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
setMessage(`Trying username variant ${match[1]}.${increment}...`)
|
|
99
|
+
login(`${match[1]}.${increment}`)
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
setMessage(`Trying username variant ${u}.1...`)
|
|
103
|
+
login(`${u}.1`)
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
setDisabledButton(false)
|
|
108
|
+
setMessage("Wrong username or password")
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<>
|
|
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>
|
|
187
|
+
</>
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export default Login
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {useEffect, useState} from "react"
|
|
2
|
+
import Card from "@mui/material/Card"
|
|
3
|
+
import CardContent from "@mui/material/CardContent"
|
|
4
|
+
import FilledInput from "@mui/material/FilledInput"
|
|
5
|
+
import IconButton from "@mui/material/IconButton"
|
|
6
|
+
import InputAdornment from "@mui/material/InputAdornment"
|
|
7
|
+
import List from "@mui/material/List"
|
|
8
|
+
import ListItem from "@mui/material/ListItem"
|
|
9
|
+
import Typography from "@mui/material/Typography"
|
|
10
|
+
import ContentCopy from "@mui/icons-material/ContentCopy"
|
|
11
|
+
|
|
12
|
+
const LoginCodes = ({user, host, code}) => {
|
|
13
|
+
const [loginCodes, setLoginCodes] = useState([])
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!user || !host || !code) return
|
|
17
|
+
|
|
18
|
+
const updated = new Map()
|
|
19
|
+
let secret = null
|
|
20
|
+
|
|
21
|
+
const update = async codes => {
|
|
22
|
+
if (!codes || !secret) return
|
|
23
|
+
|
|
24
|
+
for (const [key, enc] of Object.entries(codes)) {
|
|
25
|
+
if (!key) continue
|
|
26
|
+
|
|
27
|
+
if (enc) {
|
|
28
|
+
updated.set(key, {
|
|
29
|
+
key: key,
|
|
30
|
+
code: await user.SEA.decrypt(enc, secret),
|
|
31
|
+
})
|
|
32
|
+
} else {
|
|
33
|
+
updated.delete(key)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
setLoginCodes([...updated.values()])
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
user.get([host, "epub"], async epub => {
|
|
40
|
+
if (!epub) {
|
|
41
|
+
console.error("No epub for host!")
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
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)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
return () => {
|
|
54
|
+
user.get([host, "shared"]).next("login_codes").next(code).off(update)
|
|
55
|
+
}
|
|
56
|
+
}, [user, host, code])
|
|
57
|
+
|
|
58
|
+
const select = target => {
|
|
59
|
+
const li = target.closest("li")
|
|
60
|
+
if (li && li.childNodes[0].childNodes[0].nodeName === "INPUT") {
|
|
61
|
+
li.childNodes[0].childNodes[0].select()
|
|
62
|
+
} else {
|
|
63
|
+
console.error("not input field", li.childNodes[0].childNodes[0].nodeName)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (loginCodes.length === 0) return null
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<Card sx={{mt: 2}}>
|
|
71
|
+
<CardContent>
|
|
72
|
+
<Typography sx={{m: 1}}>
|
|
73
|
+
You have <strong>{loginCodes.length}</strong> login code
|
|
74
|
+
{loginCodes.length > 1 ? "s" : ""} you can share
|
|
75
|
+
</Typography>
|
|
76
|
+
<List dense={true} sx={{maxHeight: 300, overflow: "auto"}}>
|
|
77
|
+
{loginCodes.map(item => (
|
|
78
|
+
<ListItem key={item.key}>
|
|
79
|
+
<FilledInput
|
|
80
|
+
defaultValue={item.code}
|
|
81
|
+
readOnly={true}
|
|
82
|
+
endAdornment={
|
|
83
|
+
<InputAdornment position="end">
|
|
84
|
+
<IconButton
|
|
85
|
+
edge="end"
|
|
86
|
+
aria-label="copy login code"
|
|
87
|
+
onClick={event => select(event.target)}
|
|
88
|
+
>
|
|
89
|
+
<ContentCopy />
|
|
90
|
+
</IconButton>
|
|
91
|
+
</InputAdornment>
|
|
92
|
+
}
|
|
93
|
+
/>
|
|
94
|
+
</ListItem>
|
|
95
|
+
))}
|
|
96
|
+
</List>
|
|
97
|
+
</CardContent>
|
|
98
|
+
</Card>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export default LoginCodes
|