Changeset 303f51d
- Timestamp:
- 03/03/24 21:45:56 (15 months ago)
- Branches:
- main
- Children:
- b78c0c1
- Parents:
- a3d63eb
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/App.js
ra3d63eb r303f51d 31 31 <Route path="/reservationConfirmation/:tableNumber/:timeSlot/:restaurantId" element={<ReservationConfirmation />} /> 32 32 <Route path="/reservations/reservationEdit/:reservationId" element={<ReservationEdit />} /> 33 <Route path= {"/login"} exact render={() => <LoginForm login={this.login}/>}/>33 <Route path="/login" element={<LoginForm/>}/> 34 34 <Route path="/error" element={<ErrorPage/>}/> 35 35 </Routes> -
my-react-app/src/components/Login.js
ra3d63eb r303f51d 1 import React, {useState} from 'react'; 2 import {useNavigate} from 'react-router-dom'; 3 const AUTH_TOKEN = 'auth_token'; 1 // Login.js 2 import React, { useState } from 'react'; 3 import axios from 'axios'; 4 import {useNavigate} from "react-router-dom"; 4 5 5 const loginForm = (props) => { 6 const[auth, setAuth] = useState({}); 6 const Login = ({ onLogin }) => { 7 7 const navigate = useNavigate(); 8 const [credentials, setCredentials] = useState({ username: '', password: '' }); 8 9 9 const login = (e) => { 10 const auth = { 11 "username":e.target.username.value, 12 "password":e.target.password.value 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); 13 30 } 31 }; 14 32 15 props.login(auth, (response) => {16 debugger;17 localStorage.setItem(AUTH_TOKEN, response.data)18 navigate("/");19 })20 }21 22 const onChangeHandler = (e) => {23 const name = e.target.name;24 const value = e.target.value;25 setAuth({name:value});26 }27 33 28 34 return ( 29 35 <div> 30 <form onSubmit={login}> 31 <div className="row form-group"> 32 <div className="col-md-6 font-weight-bold"> Корисничко име:</div> 33 <div className="col-md-6"> 34 <input name={"username"} onChange={onChangeHandler} defaultValue={auth.username} type="text" 35 className="form-control"/> 36 </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 /> 37 46 </div> 38 <div className="row form-group"> 39 <div className="col-md-6 font-weight-bold"> Лозинка:</div> 40 <div className="col-md-6"> 41 <input name={"password"} onChange={onChangeHandler} defaultValue={auth.password} type="password" 42 className="form-control" 43 title="Лозинка"/> 44 </div> 47 <div> 48 <label>Password:</label> 49 <input 50 type="password" 51 name="password" 52 value={credentials.password} 53 onChange={handleChange} 54 /> 45 55 </div> 46 <div className="col-md-12 text-right"> 47 48 <button type="submit" className="btn btn-primary" title="Зачувај"> 49 <i className="fa fa-fw fa-user"></i> Логин 50 </button> 51 </div> 56 <button type="submit">Login</button> 52 57 </form> 53 58 </div> 54 ) 55 } 59 ); 60 }; 56 61 57 export default loginForm;62 export default Login; -
src/main/java/com/example/rezevirajmasa/demo/web/rest/AuthController.java
ra3d63eb r303f51d 1 1 package com.example.rezevirajmasa.demo.web.rest; 2 2 3 import com.example.rezevirajmasa.demo.model.Customer; 4 import com.example.rezevirajmasa.demo.service.CustomerService; 5 import org.apache.coyote.Response; 3 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.http.HttpStatus; 8 import org.springframework.http.ResponseEntity; 4 9 import org.springframework.security.authentication.AuthenticationManager; 10 import org.springframework.security.crypto.password.PasswordEncoder; 11 import org.springframework.web.bind.annotation.CrossOrigin; 12 import org.springframework.web.bind.annotation.PostMapping; 13 import org.springframework.web.bind.annotation.RequestBody; 5 14 import org.springframework.web.bind.annotation.RestController; 6 15 16 @CrossOrigin(origins = "http://localhost:3000/") 7 17 @RestController 8 18 public class AuthController { 19 private final CustomerService customerService; 20 private final PasswordEncoder passwordEncoder; 21 22 public AuthController(CustomerService customerService, PasswordEncoder passwordEncoder) { 23 this.customerService = customerService; 24 this.passwordEncoder = passwordEncoder; 25 } 26 27 @PostMapping("/api/login") 28 public ResponseEntity<String> login(@RequestBody Customer customer) { 29 Customer exisitngCustomer = customerService.findByEmail(customer.getEmail()); 30 31 if(passwordEncoder.matches(customer.getPassword(), exisitngCustomer.getPassword())) { 32 String token = generateToken(exisitngCustomer); 33 return ResponseEntity.ok(token); 34 } else { 35 return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); 36 } 37 } 38 39 private String generateToken(Customer customer) { 40 // Implement your token generation logic here 41 return "dummy_token"; 42 } 9 43 }
Note:
See TracChangeset
for help on using the changeset viewer.