aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Login.js
diff options
context:
space:
mode:
authorLibravatarLibravatar Biswakalyan Bhuyan <biswa@surgot.in> 2024-07-18 15:49:21 +0530
committerLibravatarLibravatar Biswakalyan Bhuyan <biswa@surgot.in> 2024-07-18 15:49:21 +0530
commitcf0421c94b8ca14e819bad45e7db6875b92d547b (patch)
tree8deb4de15449bc2cf24c88950490ce60b5f6aaa8 /src/components/Login.js
parentbe93c42fc010e7309e9e10d0431418ae5e7dbb93 (diff)
downloadadmin-panel-cf0421c94b8ca14e819bad45e7db6875b92d547b.tar.gz
admin-panel-cf0421c94b8ca14e819bad45e7db6875b92d547b.tar.bz2
admin-panel-cf0421c94b8ca14e819bad45e7db6875b92d547b.zip
added and setup dashboard login auth and many more
Diffstat (limited to 'src/components/Login.js')
-rw-r--r--src/components/Login.js39
1 files changed, 19 insertions, 20 deletions
diff --git a/src/components/Login.js b/src/components/Login.js
index 9a67051..64539c6 100644
--- a/src/components/Login.js
+++ b/src/components/Login.js
@@ -1,33 +1,32 @@
import React, { useState } from 'react';
import axios from 'axios';
+import { useHistory } from 'react-router-dom';
-const Login = ({ setAuth }) => {
- const [formData, setFormData] = useState({
- username: '',
- password: '',
- });
+const Login = () => {
+ const [email, setEmail] = useState('');
+ const [password, setPassword] = useState('');
+ const history = useHistory();
- const { username, password } = formData;
-
- const onChange = (e) => setFormData({ ...formData, [e.target.name]: e.target.value });
-
- const onSubmit = async (e) => {
+ const handleSubmit = 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);
+ const response = await axios.post('/api/auth/login', { email, password });
+ localStorage.setItem('token', response.data.token);
+ history.push('/dashboard');
+ } catch (error) {
+ console.error('Error logging in:', error);
}
};
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>
+ <div>
+ <h2>Login</h2>
+ <form onSubmit={handleSubmit}>
+ <input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} required />
+ <input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} required />
+ <button type="submit">Login</button>
+ </form>
+ </div>
);
};