Login.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { useState } from "react";
  2. import { useDispatch, useSelector } from "react-redux";
  3. import { actionLogin } from "../../redux/actions/creators/auth";
  4. import Avatar from "@mui/material/Avatar";
  5. import Button from "@mui/material/Button";
  6. import CssBaseline from "@mui/material/CssBaseline";
  7. import TextField from "@mui/material/TextField";
  8. import { Link, Navigate } from "react-router-dom";
  9. import Grid from "@mui/material/Grid";
  10. import Box from "@mui/material/Box";
  11. import LockOutlinedIcon from "@mui/icons-material/LockOutlined";
  12. import Typography from "@mui/material/Typography";
  13. import Container from "@mui/material/Container";
  14. import { createTheme, ThemeProvider } from "@mui/material/styles";
  15. import MuiAlert from "@mui/material/Alert";
  16. import Snackbar from "@mui/material/Snackbar";
  17. import { createSelector } from "reselect";
  18. import { useForm } from "react-hook-form";
  19. const theme = createTheme({
  20. palette: {
  21. mode: "dark",
  22. },
  23. });
  24. const Alert = React.forwardRef(function Alert(props, ref) {
  25. return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />;
  26. });
  27. const authState = createSelector(
  28. (state) => state.auth,
  29. (auth) => auth
  30. );
  31. const Login = () => {
  32. const dispatch = useDispatch();
  33. const { status, authToken } = useSelector(authState);
  34. const { register, handleSubmit, reset } = useForm();
  35. const [error, setError] = useState(false);
  36. const submitHandler = ({ email, password }) => {
  37. dispatch(actionLogin(email, password));
  38. reset();
  39. };
  40. const handleClose = (event, reason) => {
  41. if (reason === "clickaway") return;
  42. setError(false);
  43. };
  44. if (authToken) return <Navigate to="/" />;
  45. return (
  46. <ThemeProvider theme={theme}>
  47. <Snackbar
  48. open={error || status === "FAIL"}
  49. autoHideDuration={6000}
  50. onClose={handleClose}
  51. >
  52. <Alert
  53. onClose={handleClose}
  54. severity="error"
  55. sx={{ width: "100%" }}
  56. >
  57. Such user is not found OR the data is not correct!
  58. </Alert>
  59. </Snackbar>
  60. <Container component="main" maxWidth="xs">
  61. <CssBaseline />
  62. <Box
  63. sx={{
  64. marginTop: 8,
  65. display: "flex",
  66. flexDirection: "column",
  67. alignItems: "center",
  68. }}
  69. >
  70. <Avatar sx={{ m: 1, bgcolor: "secondary.main" }}>
  71. <LockOutlinedIcon />
  72. </Avatar>
  73. <Typography component="h1" variant="h5">
  74. Login
  75. </Typography>
  76. <Box
  77. component="form"
  78. onSubmit={handleSubmit(submitHandler)}
  79. noValidate
  80. sx={{ mt: 1 }}
  81. >
  82. <TextField
  83. margin="normal"
  84. required
  85. fullWidth
  86. id="email"
  87. label="Email Address"
  88. name="email"
  89. autoComplete="email"
  90. autoFocus
  91. {...register("email", { required: true })}
  92. />
  93. <TextField
  94. margin="normal"
  95. required
  96. fullWidth
  97. name="password"
  98. label="Password"
  99. type="password"
  100. id="password"
  101. autoComplete="current-password"
  102. {...register("password", { required: true })}
  103. />
  104. <Button
  105. type="submit"
  106. fullWidth
  107. variant="contained"
  108. sx={{ mt: 3, mb: 2 }}
  109. >
  110. Sign In
  111. </Button>
  112. <Grid container>
  113. <Grid item>
  114. <Link to="/signup" variant="body2">
  115. {"Don't have an account? Sign Up"}
  116. </Link>
  117. </Grid>
  118. </Grid>
  119. </Box>
  120. </Box>
  121. </Container>
  122. </ThemeProvider>
  123. );
  124. };
  125. export default Login;