Merge branch '17-email-login-does-not-work' into 'dev'

Resolve "Email login does not work"

Closes #17

See merge request sa4-2020/the-sanmarinoes/backend!22
This commit is contained in:
Claudio Maggioni 2020-03-04 15:17:04 +01:00
commit 033b52af18
2 changed files with 14 additions and 2 deletions

View file

@ -45,6 +45,7 @@ public class AuthenticationController {
@PostMapping("/login") @PostMapping("/login")
public JWTResponse login(@Valid @RequestBody JWTRequest authenticationRequest) public JWTResponse login(@Valid @RequestBody JWTRequest authenticationRequest)
throws Exception { throws Exception {
final UserDetails userDetails;
if (authenticationRequest.getUsernameOrEmail().contains("@")) { if (authenticationRequest.getUsernameOrEmail().contains("@")) {
// usernameOrEmail contains an email, so fetch the corresponding username // usernameOrEmail contains an email, so fetch the corresponding username
final User user = final User user =
@ -54,16 +55,19 @@ public class AuthenticationController {
throw new UserNotFoundException(); throw new UserNotFoundException();
} }
System.out.println(user);
System.out.println(authenticationRequest);
authenticate(user.getUsername(), authenticationRequest.getPassword()); authenticate(user.getUsername(), authenticationRequest.getPassword());
userDetails = userDetailsService.loadUserByUsername(user.getUsername());
} else { } else {
// usernameOrEmail contains a username, authenticate with that then // usernameOrEmail contains a username, authenticate with that then
authenticate( authenticate(
authenticationRequest.getUsernameOrEmail(), authenticationRequest.getUsernameOrEmail(),
authenticationRequest.getPassword()); authenticationRequest.getPassword());
userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsernameOrEmail());
} }
final UserDetails userDetails =
userDetailsService.loadUserByUsername(authenticationRequest.getUsernameOrEmail());
final String token = jwtTokenUtil.generateToken(userDetails); final String token = jwtTokenUtil.generateToken(userDetails);
return new JWTResponse(token); return new JWTResponse(token);
} }

View file

@ -19,4 +19,12 @@ public class JWTRequest {
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
@Override
public String toString() {
return "JWTRequest{" +
"usernameOrEmail='" + usernameOrEmail + '\'' +
", password='" + password + '\'' +
'}';
}
} }