Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 1 | import React, { useState } from 'react'; |
| 2 | import { useHistory } from "react-router-dom"; |
| 3 | |
| 4 | import { Box, Container, Fab, Card, CardContent, Typography, Input } from '@material-ui/core'; |
| 5 | import GroupAddRounded from '@material-ui/icons/GroupAddRounded'; |
| 6 | import { makeStyles } from '@material-ui/core/styles'; |
| 7 | import authManager from '../AuthManager' |
| 8 | |
| 9 | const useStyles = makeStyles((theme) => ({ |
| 10 | root: { |
| 11 | '& > *': { |
| 12 | margin: theme.spacing(1), |
| 13 | }, |
| 14 | }, |
| 15 | extendedIcon: { |
| 16 | marginRight: theme.spacing(1), |
| 17 | }, |
| 18 | wizardCard: { |
| 19 | borderRadius: 8, |
| 20 | maxWidth: 360, |
| 21 | margin: "16px auto" |
| 22 | }, textField: { |
| 23 | margin: theme.spacing(1), |
| 24 | } |
| 25 | })) |
| 26 | |
| 27 | export default function ServerSetup(props) { |
| 28 | const classes = useStyles() |
| 29 | const history = useHistory(); |
| 30 | const [password, setPassword] = useState(''); |
| 31 | const [passwordRepeat, setPasswordRepeat] = useState(''); |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 32 | const [loading, setLoading] = useState(false); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 33 | |
| 34 | const isValid = () => password && password === passwordRepeat |
| 35 | |
| 36 | const handleSubmit = async e => { |
| 37 | e.preventDefault() |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 38 | setLoading(true) |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 39 | if (!isValid()) |
| 40 | return |
| 41 | if (await authManager.setup(password)) { |
| 42 | history.replace('/') |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return ( |
| 47 | <Container className='message-list'> |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 48 | <Card className={classes.wizardCard} disabled={loading}> |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 49 | <CardContent component="form" onSubmit={handleSubmit}> |
| 50 | <Typography gutterBottom variant="h5" component="h2"> |
| 51 | Jami Web Node setup |
| 52 | </Typography> |
| 53 | <Typography variant="body2" color="textSecondary" component="p"> |
| 54 | Welcome to the Jami web node setup.<br/> |
| 55 | Let's start by creating a new administrator account to control access to the server configuration. |
| 56 | </Typography> |
| 57 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 58 | <Box style={{ textAlign: 'center', marginTop: 8, marginBottom: 16 }}> |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 59 | <div><Input className={classes.textField} value="admin" name="username" autoComplete="username" disabled /></div> |
| 60 | <div><Input |
| 61 | className={classes.textField} |
| 62 | value={password} |
| 63 | onChange={e => setPassword(e.target.value)} |
| 64 | name="password" |
| 65 | type='password' |
| 66 | placeholder="New password" |
| 67 | autoComplete="new-password" /> |
| 68 | </div> |
| 69 | <div><Input |
| 70 | className={classes.textField} |
| 71 | value={passwordRepeat} |
| 72 | onChange={e => setPasswordRepeat(e.target.value)} |
| 73 | name="password" |
| 74 | error={!!passwordRepeat && !isValid()} |
| 75 | type='password' |
| 76 | placeholder="Repeat password" |
| 77 | autoComplete="new-password" /></div> |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 78 | </Box> |
| 79 | <Box style={{ textAlign: 'center', marginTop: 24 }}> |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 80 | <Fab variant='extended' color='primary' type='submit' disabled={!isValid()}> |
| 81 | <GroupAddRounded className={classes.extendedIcon} /> |
| 82 | Create admin account |
| 83 | </Fab> |
| 84 | </Box> |
| 85 | </CardContent> |
| 86 | </Card> |
| 87 | </Container>) |
| 88 | } |