source: my-react-app/src/components/Login.js@ 303f51d

main
Last change on this file since 303f51d was 303f51d, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

RetaurantServiceImpl problemi
isAvailable od tableEntity...

  • Property mode set to 100644
File size: 1.8 KB
Line 
1// Login.js
2import React, { useState } from 'react';
3import axios from 'axios';
4import {useNavigate} from "react-router-dom";
5
6const Login = ({ onLogin }) => {
7 const navigate = useNavigate();
8 const [credentials, setCredentials] = useState({ username: '', password: '' });
9
10 const handleChange = (e) => {
11 const { name, value } = e.target;
12 setCredentials({ ...credentials, [name]: value });
13 };
14
15 const handleSubmit = async (e) => {
16 e.preventDefault();
17 try {
18 const response = await axios.post('http://localhost:8080/api/login', {
19 email: credentials.username,
20 password: credentials.password
21 });
22 const { token } = response.data;
23 // Store token securely (e.g., using HTTP cookies)
24 localStorage.setItem('token', token);
25
26 navigate("/")
27 } catch (error) {
28 // Handle login failure
29 console.error('Login failed:', error);
30 }
31 };
32
33
34 return (
35 <div>
36 <h2>Login</h2>
37 <form onSubmit={handleSubmit}>
38 <div>
39 <label>Username:</label>
40 <input
41 type="text"
42 name="username"
43 value={credentials.username}
44 onChange={handleChange}
45 />
46 </div>
47 <div>
48 <label>Password:</label>
49 <input
50 type="password"
51 name="password"
52 value={credentials.password}
53 onChange={handleChange}
54 />
55 </div>
56 <button type="submit">Login</button>
57 </form>
58 </div>
59 );
60};
61
62export default Login;
Note: See TracBrowser for help on using the repository browser.