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