fixed login

This commit is contained in:
britea 2020-03-04 15:11:23 +01:00
parent b6add2aaf6
commit 8c1a0db920
2 changed files with 14 additions and 2 deletions

View file

@ -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);
}

View file

@ -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 + '\'' +
'}';
}
}