Revize 8370b6c1
Přidáno uživatelem Václav Honzík před téměř 3 roky(ů)
frontend/src/features/Auth/Login.tsx | ||
---|---|---|
1 |
import { Button, TextField, Typography } from '@mui/material' |
|
2 |
import { useFormik } from 'formik' |
|
3 |
import { Fragment, useEffect } from 'react' |
|
4 |
import { useDispatch, useSelector } from 'react-redux' |
|
5 |
import { useNavigate } from 'react-router-dom' |
|
6 |
import * as yup from 'yup' |
|
7 |
import { SchemaOf } from 'yup' |
|
8 |
import { PasswordDto, UserDto } from '../../swagger/data-contracts' |
|
9 |
import { RootState } from '../redux/store' |
|
10 |
import { logIn } from './userThunks' |
|
11 |
|
|
12 |
interface LoginFields { |
|
13 |
username: string |
|
14 |
password: string |
|
15 |
} |
|
1 | 16 |
|
2 | 17 |
const Login = () => { |
18 |
const validationSchema: SchemaOf<LoginFields> = yup.object().shape({ |
|
19 |
username: yup.string().required('Username is required'), |
|
20 |
password: yup.string().required('Password is required'), |
|
21 |
}) |
|
22 |
|
|
23 |
const dispatch = useDispatch() |
|
24 |
const formik = useFormik({ |
|
25 |
initialValues: { |
|
26 |
username: '', |
|
27 |
password: '', |
|
28 |
}, |
|
29 |
validationSchema, |
|
30 |
onSubmit: () => { |
|
31 |
dispatch( |
|
32 |
logIn({ |
|
33 |
name: formik.values.username, |
|
34 |
passwords: { |
|
35 |
password: formik.values.password, |
|
36 |
confirmationPassword: '', |
|
37 |
} as PasswordDto, |
|
38 |
} as UserDto) |
|
39 |
) |
|
40 |
}, |
|
41 |
}) |
|
42 |
|
|
43 |
// Redirect to home if the user is logged in |
|
44 |
const userLoggedIn = useSelector( |
|
45 |
(state: RootState) => state.user.isLoggedIn |
|
46 |
) |
|
47 |
const navigate = useNavigate() |
|
48 |
useEffect(() => { |
|
49 |
if (userLoggedIn) { |
|
50 |
navigate('/') |
|
51 |
} |
|
52 |
}, [userLoggedIn, navigate]) |
|
3 | 53 |
|
4 | 54 |
return ( |
5 |
<> |
|
55 |
<Fragment> |
|
56 |
<Typography variant="h3">Login</Typography> |
|
57 |
<p>Credentials = admin:password</p> |
|
6 | 58 |
|
7 |
</> |
|
59 |
<form onSubmit={formik.handleSubmit}> |
|
60 |
<TextField |
|
61 |
label="Username" |
|
62 |
name="username" |
|
63 |
fullWidth |
|
64 |
sx={{ mb: 2 }} |
|
65 |
value={formik.values.username} |
|
66 |
onChange={formik.handleChange} |
|
67 |
error={ |
|
68 |
Boolean(formik.errors.username) && |
|
69 |
formik.touched.username |
|
70 |
} |
|
71 |
helperText={ |
|
72 |
formik.errors.username && |
|
73 |
formik.touched.username && |
|
74 |
formik.errors.username |
|
75 |
} |
|
76 |
/> |
|
77 |
<TextField |
|
78 |
type="password" |
|
79 |
label="Password" |
|
80 |
name="password" |
|
81 |
fullWidth |
|
82 |
value={formik.values.password} |
|
83 |
onChange={formik.handleChange} |
|
84 |
error={ |
|
85 |
Boolean(formik.errors.password) && |
|
86 |
formik.touched.password |
|
87 |
} |
|
88 |
helperText={ |
|
89 |
formik.errors.password && |
|
90 |
formik.touched.password && |
|
91 |
formik.errors.password |
|
92 |
} |
|
93 |
sx={{ mb: 2 }} |
|
94 |
/> |
|
95 |
<Button |
|
96 |
size="large" |
|
97 |
variant="contained" |
|
98 |
color="primary" |
|
99 |
type="submit" |
|
100 |
fullWidth |
|
101 |
> |
|
102 |
Login |
|
103 |
</Button> |
|
104 |
</form> |
|
105 |
</Fragment> |
|
8 | 106 |
) |
9 | 107 |
} |
10 | 108 |
|
11 |
export default Login |
|
109 |
export default Login |
Také k dispozici: Unified diff
login simple form impl + slice for user state