diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Login.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/components/Login.js b/src/components/Login.js new file mode 100644 index 0000000..9a67051 --- /dev/null +++ b/src/components/Login.js @@ -0,0 +1,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; |