added LoginController Post(/login)
This commit is contained in:
		@ -0,0 +1,45 @@
 | 
			
		||||
package ovh.herisson.Clyde.EndPoints;
 | 
			
		||||
import org.springframework.http.HttpHeaders;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.http.ResponseEntity;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
import ovh.herisson.Clyde.Services.TokenService;
 | 
			
		||||
import ovh.herisson.Clyde.Services.UserService;
 | 
			
		||||
import ovh.herisson.Clyde.Tables.User;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
			
		||||
public class LoginController {
 | 
			
		||||
    private final UserService userService;
 | 
			
		||||
    private final TokenService tokenService;
 | 
			
		||||
 | 
			
		||||
    public LoginController(UserService userService, TokenService tokenService){
 | 
			
		||||
        this.userService =userService;
 | 
			
		||||
        this.tokenService = tokenService;
 | 
			
		||||
    }
 | 
			
		||||
    @PostMapping("/login")
 | 
			
		||||
    public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate){
 | 
			
		||||
 | 
			
		||||
        User user = userService.getUser(identifier);
 | 
			
		||||
        if (user == null){
 | 
			
		||||
            return new ResponseEntity<String>("wrong ID or Email", HttpStatus.BAD_REQUEST);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!userService.checkPassword(user,password)){
 | 
			
		||||
            return new ResponseEntity<String>("wrong Password",HttpStatus.BAD_REQUEST);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        String token = tokenService.generateNewToken();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        tokenService.saveToken(token,user,expirationDate);
 | 
			
		||||
 | 
			
		||||
        HttpHeaders responseHeaders = new HttpHeaders();
 | 
			
		||||
        responseHeaders.set("Set-Cookie",String.format("session_token=%s",token));
 | 
			
		||||
        return ResponseEntity.ok().headers(responseHeaders).build();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user