blob: 9a67051f2e5e2423347abe6ebbb139f4901fa5d6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import React, { useState } from 'react';
import axios from 'axios';
const Login = ({ setAuth }) => {
const [formData, setFormData] = useState({
username: '',
password: '',
});
const { username, password } = formData;
const onChange = (e) => setFormData({ ...formData, [e.target.name]: e.target.value });
const onSubmit = async (e) => {
e.preventDefault();
try {
const res = await axios.post('/api/auth/login', formData);
localStorage.setItem('token', res.data.token);
setAuth(true);
} catch (err) {
console.error(err.response.data);
}
};
return (
<form onSubmit={onSubmit}>
<input type="text" name="username" value={username} onChange={onChange} required />
<input type="password" name="password" value={password} onChange={onChange} required />
<button type="submit">Login</button>
</form>
);
};
export default Login;
|