From 8c1a0db9204c6f331eab1d9db607d71d185a4894 Mon Sep 17 00:00:00 2001 From: britea Date: Wed, 4 Mar 2020 15:11:23 +0100 Subject: [PATCH] fixed login --- .../smarthut/controller/AuthenticationController.java | 8 ++++++-- .../usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java index dd23fba..c0c83c2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AuthenticationController.java @@ -45,6 +45,7 @@ public class AuthenticationController { @PostMapping("/login") public JWTResponse login(@Valid @RequestBody JWTRequest authenticationRequest) throws Exception { + final UserDetails userDetails; if (authenticationRequest.getUsernameOrEmail().contains("@")) { // usernameOrEmail contains an email, so fetch the corresponding username final User user = @@ -54,16 +55,19 @@ public class AuthenticationController { throw new UserNotFoundException(); } + System.out.println(user); + System.out.println(authenticationRequest); + authenticate(user.getUsername(), authenticationRequest.getPassword()); + userDetails = userDetailsService.loadUserByUsername(user.getUsername()); } else { // usernameOrEmail contains a username, authenticate with that then authenticate( authenticationRequest.getUsernameOrEmail(), authenticationRequest.getPassword()); + userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsernameOrEmail()); } - final UserDetails userDetails = - userDetailsService.loadUserByUsername(authenticationRequest.getUsernameOrEmail()); final String token = jwtTokenUtil.generateToken(userDetails); return new JWTResponse(token); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java index a032c05..52fa5c3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java @@ -19,4 +19,12 @@ public class JWTRequest { public void setPassword(String password) { this.password = password; } + + @Override + public String toString() { + return "JWTRequest{" + + "usernameOrEmail='" + usernameOrEmail + '\'' + + ", password='" + password + '\'' + + '}'; + } }