blob: 64539c69550e8f2e5bb1f392a37ebee08ad18e26 (
plain) (
tree)
|
|
import React, { useState } from 'react';
import axios from 'axios';
import { useHistory } from 'react-router-dom';
const Login = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const history = useHistory();
const handleSubmit = async (e) => {
e.preventDefault();
try {
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 (
<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>
);
};
export default Login;
|