- Timestamp:
- 03/03/24 21:45:56 (15 months ago)
- Branches:
- main
- Children:
- b78c0c1
- Parents:
- a3d63eb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.