Login.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import * as React from 'react';
  2. import {Avatar, Checkbox, FormControlLabel, Grid, Link, Stack} from "@mui/material";
  3. import Typography from "@mui/material/Typography";
  4. import Container from "@mui/material/Container";
  5. import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
  6. import TextField from "@mui/material/TextField";
  7. import Button from "@mui/material/Button";
  8. import Box from "@mui/material/Box";
  9. import api from "../app/Api";
  10. import {useNavigate} from "react-router-dom";
  11. import routes from "./routes";
  12. import session from "../app/Session";
  13. const Copyright = (props) => {
  14. return (
  15. <Typography variant="body2" color="text.secondary" align="center" {...props}>
  16. {'Copyright © '}
  17. <Link color="inherit" href="https://mui.com/">
  18. Your Website
  19. </Link>{' '}
  20. {new Date().getFullYear()}
  21. {'.'}
  22. </Typography>
  23. );
  24. };
  25. const Login = () => {
  26. const handleSubmit = async (event) => {
  27. event.preventDefault();
  28. const data = new FormData(event.currentTarget);
  29. const user = {
  30. username: data.get('username'),
  31. password: data.get('password'),
  32. }
  33. const token = await api.login("http://localhost:2586"/*window.location.origin*/, user);
  34. console.log(`[Api] User auth for user ${user.username} successful, token is ${token}`);
  35. session.store(user.username, token);
  36. window.location.href = routes.app;
  37. };
  38. return (
  39. <>
  40. <Box
  41. sx={{
  42. marginTop: 8,
  43. display: 'flex',
  44. flexDirection: 'column',
  45. alignItems: 'center',
  46. }}
  47. >
  48. <Avatar sx={{m: 1, bgcolor: 'secondary.main'}}>
  49. <LockOutlinedIcon/>
  50. </Avatar>
  51. <Typography component="h1" variant="h5">
  52. Sign in
  53. </Typography>
  54. <Box component="form" onSubmit={handleSubmit} noValidate sx={{mt: 1}}>
  55. <TextField
  56. margin="normal"
  57. required
  58. fullWidth
  59. id="username"
  60. label="Username"
  61. name="username"
  62. autoFocus
  63. />
  64. <TextField
  65. margin="normal"
  66. required
  67. fullWidth
  68. name="password"
  69. label="Password"
  70. type="password"
  71. id="password"
  72. autoComplete="current-password"
  73. />
  74. <FormControlLabel
  75. control={<Checkbox value="remember" color="primary"/>}
  76. label="Remember me"
  77. />
  78. <Button
  79. type="submit"
  80. fullWidth
  81. variant="contained"
  82. sx={{mt: 3, mb: 2}}
  83. >
  84. Sign In
  85. </Button>
  86. <Grid container>
  87. <Grid item xs>
  88. <Link href="#" variant="body2">
  89. Forgot password?
  90. </Link>
  91. </Grid>
  92. <Grid item>
  93. <Link href="#" variant="body2">
  94. {"Don't have an account? Sign Up"}
  95. </Link>
  96. </Grid>
  97. </Grid>
  98. </Box>
  99. </Box>
  100. <Copyright sx={{mt: 8, mb: 4}}/>
  101. </>
  102. );
  103. }
  104. export default Login;