Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LoginForm.tsx 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import {
  2. Box,
  3. Button,
  4. Container,
  5. Grid,
  6. IconButton,
  7. InputAdornment,
  8. TextField,
  9. Typography,
  10. } from '@mui/material';
  11. import { useFormik } from 'formik';
  12. import { signIn } from 'next-auth/react';
  13. import { useTranslation } from 'next-i18next';
  14. import Link from 'next/link';
  15. import { useRouter } from 'next/router';
  16. import { useState } from 'react';
  17. import {
  18. BASE_PAGE,
  19. FORGOT_PASSWORD_PAGE,
  20. REGISTER_PAGE,
  21. } from '../../../constants/pages';
  22. import { loginSchema } from '../../../schemas/loginSchema';
  23. import ErrorMessageComponent from '../../mui/ErrorMessageComponent';
  24. interface Values {
  25. username: string;
  26. password: string;
  27. }
  28. const LoginForm = () => {
  29. const { t } = useTranslation(['forms', 'login']);
  30. const [showPassword, setShowPassword] = useState(false);
  31. const handleClickShowPassword = () => setShowPassword(!showPassword);
  32. const handleMouseDownPassword = () => setShowPassword(!showPassword);
  33. const router = useRouter();
  34. const [error, setError] = useState({ hasError: false, errorMessage: '' });
  35. const submitHandler = async (values: Values) => {
  36. const result = await signIn('credentials', {
  37. redirect: false,
  38. username: values.username,
  39. password: values.password,
  40. });
  41. if (!result?.error) {
  42. router.replace(BASE_PAGE);
  43. } else {
  44. setError({ hasError: true, errorMessage: result?.error });
  45. }
  46. };
  47. const formik = useFormik({
  48. initialValues: {
  49. username: '',
  50. password: '',
  51. },
  52. validationSchema: loginSchema,
  53. onSubmit: submitHandler,
  54. validateOnBlur: true,
  55. enableReinitialize: true,
  56. });
  57. return (
  58. <Container component="main" maxWidth="md">
  59. <Box
  60. sx={{
  61. marginTop: 32,
  62. display: 'flex',
  63. flexDirection: 'column',
  64. alignItems: 'center',
  65. }}
  66. >
  67. <Typography component="h1" variant="h5">
  68. {t('login:Title')}
  69. </Typography>
  70. {error.hasError && <ErrorMessageComponent error={error.errorMessage} />}
  71. <Box
  72. component="form"
  73. onSubmit={formik.handleSubmit}
  74. sx={{ position: 'relative', mt: 1, p: 1 }}
  75. >
  76. <TextField
  77. name="username"
  78. label={t('forms:Username')}
  79. margin="normal"
  80. value={formik.values.username}
  81. onChange={formik.handleChange}
  82. error={formik.touched.username && Boolean(formik.errors.username)}
  83. helperText={formik.touched.username && formik.errors.username}
  84. autoFocus
  85. fullWidth
  86. />
  87. <TextField
  88. name="password"
  89. label={t('forms:Password')}
  90. margin="normal"
  91. type={showPassword ? 'text' : 'password'}
  92. value={formik.values.password}
  93. onChange={formik.handleChange}
  94. error={formik.touched.password && Boolean(formik.errors.password)}
  95. helperText={formik.touched.password && formik.errors.password}
  96. fullWidth
  97. InputProps={{
  98. endAdornment: (
  99. <InputAdornment position="end">
  100. <IconButton
  101. onClick={handleClickShowPassword}
  102. onMouseDown={handleMouseDownPassword}
  103. ></IconButton>
  104. </InputAdornment>
  105. ),
  106. }}
  107. />
  108. <Button
  109. type="submit"
  110. variant="contained"
  111. sx={{ mt: 3, mb: 2 }}
  112. fullWidth
  113. >
  114. {t('login:LoginBtn')}
  115. </Button>
  116. <Grid container>
  117. <Grid
  118. item
  119. xs={12}
  120. md={6}
  121. sx={{ textAlign: { xs: 'center', md: 'left' }, mt: 1 }}
  122. >
  123. <Link href={FORGOT_PASSWORD_PAGE}>
  124. <Typography sx={{ cursor: 'pointer' }}>
  125. {t('login:ForgotPassword')}
  126. </Typography>
  127. </Link>
  128. </Grid>
  129. <Grid
  130. item
  131. xs={12}
  132. md={6}
  133. sx={{
  134. textAlign: {
  135. xs: 'center',
  136. md: 'right',
  137. },
  138. mt: 1,
  139. }}
  140. >
  141. <Link href={REGISTER_PAGE}>
  142. <Typography sx={{ cursor: 'pointer' }}>
  143. {t('login:NoAccount')}
  144. </Typography>
  145. </Link>
  146. </Grid>
  147. </Grid>
  148. </Box>
  149. </Box>
  150. </Container>
  151. );
  152. };
  153. export default LoginForm;