index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import React from 'react';
  2. import Avatar from '@material-ui/core/Avatar';
  3. import { Button } from '@material-ui/core';
  4. import CssBaseline from '@material-ui/core/CssBaseline';
  5. import TextField from '@material-ui/core/TextField';
  6. import Link from '@material-ui/core/Link';
  7. import Grid from '@material-ui/core/Grid';
  8. import Box from '@material-ui/core/Box';
  9. import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
  10. import Typography from '@material-ui/core/Typography';
  11. import { useSelector } from 'react-redux';
  12. import { makeStyles } from '@material-ui/core/styles';
  13. import Container from '@material-ui/core/Container';
  14. import { useState } from 'react';
  15. import { store, actionAuthLogin, history } from '../../App.js';
  16. import VisibilityIcon from '@material-ui/icons/Visibility';
  17. import InputAdornment from '@mui/material/InputAdornment';
  18. import { IconButton } from '@material-ui/core';
  19. import { actionLogin } from '../../actions';
  20. import VisibilityOffIcon from '@material-ui/icons/VisibilityOff';
  21. function Copyright() {
  22. return (
  23. <Typography variant="body2" color="textSecondary" align="center">
  24. {'Anton project © '}
  25. <Link color="inherit" href="https://mui.com/">
  26. OLX
  27. </Link>{' '}
  28. {new Date().getFullYear()}
  29. {'.'}
  30. </Typography>
  31. );
  32. }
  33. const useStyles = makeStyles((theme) => ({
  34. paper: {
  35. marginTop: theme.spacing(12),
  36. display: 'flex',
  37. flexDirection: 'column',
  38. alignItems: 'center',
  39. },
  40. avatar: {
  41. margin: theme.spacing(1),
  42. backgroundColor: theme.palette.secondary.main,
  43. },
  44. form: {
  45. width: '100%', // Fix IE 11 issue.
  46. marginTop: theme.spacing(1),
  47. },
  48. submit: {
  49. margin: theme.spacing(3, 0, 2),
  50. },
  51. }));
  52. function SignIn() {
  53. const classes = useStyles();
  54. const [text, setText] = useState({ login: '', pass: '' })
  55. const [inputPass, setInputPass] = useState('password')
  56. const [jwtToken, setJwtToken] = useState(false)
  57. async function buttonOnckick() {
  58. const token = await store.dispatch(actionLogin(text.login, text.pass))
  59. if (token) {
  60. store.dispatch(actionAuthLogin(token));
  61. setJwtToken(false)
  62. history.push("/page/1")
  63. }else{
  64. setJwtToken(true)
  65. }
  66. }
  67. const enterHandler = (event) => {
  68. if (event.key === 'Enter') {
  69. buttonOnckick()
  70. }
  71. }
  72. const seePassword = () => {
  73. if (inputPass === 'password') {
  74. setInputPass('text')
  75. } else {
  76. setInputPass('password')
  77. }
  78. }
  79. return (
  80. <Container component="main" maxWidth="xs" >
  81. <CssBaseline />
  82. <div className={classes.paper}>
  83. <Avatar className={classes.avatar}>
  84. <LockOutlinedIcon />
  85. </Avatar>
  86. <Typography component="h1" variant="h5">
  87. Sign in
  88. </Typography>
  89. <form className={classes.form} noValidate>
  90. <TextField
  91. onKeyPress={enterHandler}
  92. value={text.login}
  93. onChange={e => setText({ ...text, login: e.target.value })}
  94. variant="outlined"
  95. margin="normal"
  96. required
  97. fullWidth
  98. id="email"
  99. label="Email Address"
  100. name="email"
  101. autoComplete="email"
  102. autoFocus
  103. />
  104. <TextField
  105. onKeyPress={enterHandler}
  106. value={text.pass}
  107. onChange={e => setText({ ...text, pass: e.target.value })}
  108. variant="outlined"
  109. margin="normal"
  110. required
  111. fullWidth
  112. InputProps={{
  113. endAdornment: <InputAdornment position="end">
  114. <IconButton onClick={seePassword}>
  115. {inputPass !== 'password' ? <VisibilityOffIcon /> : <VisibilityIcon />}
  116. </IconButton>
  117. </InputAdornment>,
  118. }}
  119. name="password"
  120. label="Password"
  121. type={inputPass}
  122. id="password"
  123. autoComplete="current-password"
  124. />
  125. <Typography variant="body2" color="secondary" >{jwtToken && 'Please enter a valid Email and/or password.'}</Typography>
  126. <Button
  127. onClick={buttonOnckick}
  128. fullWidth
  129. variant="contained"
  130. color="primary"
  131. className={classes.submit}
  132. >
  133. Sign In
  134. </Button>
  135. <Grid container>
  136. <Grid item xs>
  137. <Link href="#" variant="body2">
  138. Forgot password?
  139. </Link>
  140. </Grid>
  141. <Grid item>
  142. <Link href="#" variant="body2">
  143. {"Don't have an account? Sign Up"}
  144. </Link>
  145. </Grid>
  146. </Grid>
  147. </form>
  148. </div>
  149. <Box mt={8}>
  150. <Copyright />
  151. </Box>
  152. </Container>
  153. );
  154. }
  155. export default SignIn