Improve code.

main
Paulo Gustavo Veiga 2024-03-24 08:33:06 -07:00
parent e99424db01
commit ede2fb7622
2 changed files with 17 additions and 13 deletions

View File

@ -56,8 +56,12 @@ public class OAuth2Controller extends BaseController {
@RequestMapping(method = RequestMethod.POST, value = "googlecallback", produces = {"application/json"})
@ResponseStatus(value = HttpStatus.OK)
public RestOath2CallbackResponse processGoogleCallback(@NotNull @RequestParam String code, @NotNull HttpServletResponse response, @NotNull HttpServletRequest request) throws WiseMappingException {
final Account user = userService.createAndAuthUserFromGoogle(code);
logger.debug("processGoogleCallback:" + code);
if (code == null) {
throw new WiseMappingException("Illegal argument exception: " + code);
}
final Account user = userService.createAndAuthUserFromGoogle(code);
String jwtToken = null;
if (user.getGoogleSync()) {
jwtToken = jwtTokenUtil.doLogin(response, user.getEmail());
@ -75,8 +79,8 @@ public class OAuth2Controller extends BaseController {
@RequestMapping(method = RequestMethod.PUT, value = "confirmaccountsync", produces = {"application/json"})
@ResponseStatus(value = HttpStatus.OK)
public RestOath2CallbackResponse confirmAccountSync(@NotNull @RequestParam String email, @NotNull @RequestParam String code, @NotNull HttpServletResponse response) throws WiseMappingException {
logger.debug("confirmAccountSync:" + email + " - " + code);
if (email == null || code == null) {
logger.debug("ConfirmAccountSync:" + email + " - " + code);
if (code == null) {
throw new WiseMappingException("Illegal argument exception: " + email + " - " + code);
}
@ -84,7 +88,7 @@ public class OAuth2Controller extends BaseController {
final Account user = userService.createAndAuthUserFromGoogle(code);
// Update login
userService.confirmAccountSync(email, code);
userService.confirmAccountSync(user.getEmail(), code);
// Add header ...
final String jwtToken = jwtTokenUtil.doLogin(response, email);

View File

@ -187,6 +187,7 @@ public class UserServiceImpl
throw new OAuthAuthenticationException(e);
}
// Callback is successful, the email of the user exits. Is an existing account ?
Account result = userManager.getUserBy(data.getEmail());
if (result == null) {
Account newUser = new Account();
@ -198,18 +199,17 @@ public class UserServiceImpl
newUser.setAuthenticationType(AuthenticationType.GOOGLE_OAUTH2);
newUser.setGoogleToken(data.getAccessToken());
result = this.createUser(newUser, false, true);
} else {
// user exists and doesn't have confirmed account linking, I must wait for confirmation
if (result.getGoogleSync() == null) {
result.setGoogleSync(false);
result.setSyncCode(callbackCode);
result.setGoogleToken(data.getAccessToken());
userManager.updateUser(result);
}
logger.debug("Google account successfully created");
}
// Is the user a non-oauth user ?
if (result.getGoogleSync() == null || !result.getGoogleSync()) {
result.setGoogleSync(false);
result.setSyncCode(callbackCode);
result.setGoogleToken(data.getAccessToken());
userManager.updateUser(result);
}
return result;
}
public Account confirmAccountSync(@NotNull String email, @NotNull String code) throws WiseMappingException {