admin/login: ip-change option
Massimo Melina committed
Oct 6, 2024 at 10:46 UTC
581b0c98dbed92e3ca81fe07208d92b4d783bf9a
2 files changed
+12
-7
admin/src/LoginRequired.ts
+11
-6
@@ -2,8 +2,8 @@
2
3
import { state, useSnapState } from './state'
4
import { createElement as h, Fragment, useEffect, useRef, useState } from 'react'
5
-import { getHFS, HTTP_FORBIDDEN, HTTP_UNAUTHORIZED, makeSessionRefresher } from './misc'
6
-import { Form } from '@hfs/mui-grid-form'
5
+import { CFG, HTTP_FORBIDDEN, HTTP_UNAUTHORIZED, makeSessionRefresher } from './misc'
6
+import { BoolField, Form } from '@hfs/mui-grid-form'
7
import { apiCall } from './api'
8
import { srpClientSequence } from '@hfs/shared'
9
import { Alert, Box } from '@mui/material'
@@ -22,7 +22,7 @@ export function LoginRequired({ children }: any) {
22
}
23
24
function LoginForm() {
25
- const [values, setValues] = useState({ username: '', password: '' })
25
+ const [values, setValues] = useState({ username: '', password: '', ipChange: false })
26
const [error, setError] = useState('')
27
const formRef = useRef<HTMLFormElement>()
28
const empty = formRef.current?.querySelector('input[value=""]')
@@ -31,12 +31,15 @@ function LoginForm() {
31
h(Form, {
32
formRef,
33
values,
34
+ m: 2,
35
+ maxWidth: '25em',
36
set(v, k) {
37
setValues(values => ({ ...values, [k]: v }))
38
},
39
fields: [
40
{ k: 'username', autoComplete: 'username', autoFocus: true, required: true },
41
{ k: 'password', type: 'password', autoComplete: 'current-password', required: true },
42
+ { k: 'ipChange', comp: BoolField, label: "Allow IP change during this session" },
43
],
44
addToBar: [ error && h(Alert, { severity: 'error', sx: { flex: 1 } }, error) ],
45
saveOnEnter: true,
@@ -46,7 +49,9 @@ function LoginForm() {
49
async onClick() {
50
try {
51
setError('')
49
- await login(values.username, values.password)
52
+ await login(values.username, values.password, {
53
+ [CFG.allow_session_ip_change]: values.ipChange
54
+ })
55
}
56
catch(e) {
57
setError(String(e))
@@ -57,8 +62,8 @@ function LoginForm() {
62
)
63
}
64
60
-async function login(username: string, password: string) {
61
- const res = await srpClientSequence(username, password, apiCall).catch(err => {
65
+async function login(username: string, password: string, extra?: object) {
66
+ const res = await srpClientSequence(username, password, apiCall, extra).catch(err => {
67
throw err?.code === HTTP_UNAUTHORIZED ? "Wrong username or password"
68
: err === 'trust' ? "Login aborted: server identity cannot be trusted"
69
: err?.name === 'AbortError' ? "Server didn't respond"
mui-grid-form/misc-fields.ts
+1
-1
@@ -70,7 +70,7 @@ export function BoolField({ label='', value, onChange, setApi, helperText, error
70
}
71
})
72
return h(Box, { ml: 1, sx: error ? { color: 'error.main', outlineOffset: 6, outline: '1px solid' } : undefined },
73
- h(FormControlLabel, { label, control, labelPlacement: 'end', ...props.size==='small' && { sx: { '& .MuiFormControlLabel-label': { fontSize: '.9rem' } } } }),
73
+ h(FormControlLabel, { label, control, labelPlacement: 'end', sx: { mr: 0, ...props.size==='small' && { '& .MuiFormControlLabel-label': { fontSize: '.9rem' } } } }),
74
helperText && h(FormHelperText, { sx: { mt: 0 }, error }, helperText)
75
)
76
}