This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Login.js
62 lines (51 loc) · 1.51 KB
/
Login.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import './Login.css'
import axios from 'axios';
import useProtectedRoute from './ProtectRoutes';
const LoginPage = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
const navigate = useNavigate();
const handleLogin = async (e) => {
e.preventDefault();
try {
const response = await axios.post('http://djangoEB-env.eba-iprn24hb.us-west-2.elasticbeanstalk.com/login/', {
username,
password,
});
const { token } = response.data;
localStorage.setItem('token', token);
navigate('/readPrescription');
} catch (error) {
setError('Invalid username or password');
}
};
return (
<div className='container'>
<form className='login-form' onSubmit={handleLogin}>
<h2>Login</h2>
{error && <p className = "error"> {error}</p>}
<div>
<label>Username:</label>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>
<div>
<label>Password:</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<button type="submit" >Login</button>
</form>
</div>
);
};
export default LoginPage;