fixed redirects
This commit is contained in:
parent
4281cbaf29
commit
b283a62d0d
5 changed files with 44 additions and 32 deletions
|
@ -30,17 +30,21 @@ public class EmailConfigurationService {
|
||||||
*/
|
*/
|
||||||
@NotNull private String registrationPath;
|
@NotNull private String registrationPath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URL to follow for password reset email confirmation. Has to end with the start of a query
|
||||||
|
* parameter
|
||||||
|
*/
|
||||||
|
@NotNull private String resetPasswordPath;
|
||||||
|
|
||||||
/** The email subject for a reset password email */
|
/** The email subject for a reset password email */
|
||||||
@NotNull private String resetPasswordSubject;
|
@NotNull private String resetPasswordSubject;
|
||||||
|
|
||||||
/** The text in the email body preceding the confirmation URL for a reset password email */
|
/** The text in the email body preceding the confirmation URL for a reset password email */
|
||||||
@NotNull private String resetPassword;
|
@NotNull private String resetPassword;
|
||||||
|
|
||||||
/**
|
@NotNull private String resetPasswordRedirect;
|
||||||
* The URL to follow for password reset email confirmation. Has to end with the start of a query
|
|
||||||
* parameter
|
@NotNull private String registrationRedirect;
|
||||||
*/
|
|
||||||
@NotNull private String resetPasswordPath;
|
|
||||||
|
|
||||||
public String getRegistrationSubject() {
|
public String getRegistrationSubject() {
|
||||||
return registrationSubject;
|
return registrationSubject;
|
||||||
|
@ -89,4 +93,20 @@ public class EmailConfigurationService {
|
||||||
public void setResetPasswordPath(String resetPasswordPath) {
|
public void setResetPasswordPath(String resetPasswordPath) {
|
||||||
this.resetPasswordPath = resetPasswordPath;
|
this.resetPasswordPath = resetPasswordPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getResetPasswordRedirect() {
|
||||||
|
return resetPasswordRedirect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResetPasswordRedirect(String resetPasswordRedirect) {
|
||||||
|
this.resetPasswordRedirect = resetPasswordRedirect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRegistrationRedirect() {
|
||||||
|
return registrationRedirect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistrationRedirect(String registrationRedirect) {
|
||||||
|
this.registrationRedirect = registrationRedirect;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTRequest;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTRequest;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserUpdateRequest;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
import io.swagger.annotations.Authorization;
|
|
||||||
import java.security.Principal;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
@ -72,21 +69,6 @@ public class AuthenticationController {
|
||||||
return new JWTResponse(token);
|
return new JWTResponse(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Authorization(value = "Bearer")
|
|
||||||
@PatchMapping("/update")
|
|
||||||
public User update(
|
|
||||||
@Valid @RequestBody final UserUpdateRequest userData, final Principal principal) {
|
|
||||||
final User oldUser = userRepository.findByUsername(principal.getName());
|
|
||||||
if (userData.getName() != null) oldUser.setName(userData.getName());
|
|
||||||
if (userData.getEmail() != null) {
|
|
||||||
oldUser.setEmail(userData.getEmail());
|
|
||||||
// TODO: handle email verification
|
|
||||||
}
|
|
||||||
if (userData.getPassword() != null)
|
|
||||||
oldUser.setPassword(encoder.encode(userData.getPassword()));
|
|
||||||
return userRepository.save(oldUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void authenticate(String username, String password) throws UnauthorizedException {
|
private void authenticate(String username, String password) throws UnauthorizedException {
|
||||||
try {
|
try {
|
||||||
authenticationManager.authenticate(
|
authenticationManager.authenticate(
|
||||||
|
|
|
@ -13,6 +13,8 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.EmailSenderService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.EmailSenderService;
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
@ -158,8 +160,10 @@ public class UserAccountController {
|
||||||
* @throws EmailTokenNotFoundException if given token is not a valid token for password reset
|
* @throws EmailTokenNotFoundException if given token is not a valid token for password reset
|
||||||
*/
|
*/
|
||||||
@PutMapping("/reset-password")
|
@PutMapping("/reset-password")
|
||||||
public OkResponse resetPassword(@Valid @RequestBody PasswordResetRequest resetRequest)
|
public void resetPassword(
|
||||||
throws EmailTokenNotFoundException {
|
@Valid @RequestBody PasswordResetRequest resetRequest,
|
||||||
|
final HttpServletResponse response)
|
||||||
|
throws EmailTokenNotFoundException, IOException {
|
||||||
final ConfirmationToken token =
|
final ConfirmationToken token =
|
||||||
confirmationTokenRepository.findByConfirmationToken(
|
confirmationTokenRepository.findByConfirmationToken(
|
||||||
resetRequest.getConfirmationToken());
|
resetRequest.getConfirmationToken());
|
||||||
|
@ -175,7 +179,7 @@ public class UserAccountController {
|
||||||
// Delete token to prevent further password changes
|
// Delete token to prevent further password changes
|
||||||
confirmationTokenRepository.delete(token);
|
confirmationTokenRepository.delete(token);
|
||||||
|
|
||||||
return new OkResponse();
|
response.sendRedirect(emailConfig.getResetPasswordRedirect());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -187,16 +191,17 @@ public class UserAccountController {
|
||||||
* confirmation
|
* confirmation
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/confirm-account")
|
@GetMapping(value = "/confirm-account")
|
||||||
public OkResponse confirmUserAccount(@RequestParam("token") @NotNull String confirmationToken)
|
public void confirmUserAccount(
|
||||||
throws EmailTokenNotFoundException {
|
@RequestParam("token") @NotNull String confirmationToken,
|
||||||
|
final HttpServletResponse response)
|
||||||
|
throws EmailTokenNotFoundException, IOException {
|
||||||
final ConfirmationToken token =
|
final ConfirmationToken token =
|
||||||
confirmationTokenRepository.findByConfirmationToken(confirmationToken);
|
confirmationTokenRepository.findByConfirmationToken(confirmationToken);
|
||||||
|
|
||||||
if (token != null && !token.getResetPassword()) {
|
if (token != null && !token.getResetPassword()) {
|
||||||
token.getUser().setEnabled(true);
|
token.getUser().setEnabled(true);
|
||||||
userRepository.save(token.getUser());
|
userRepository.save(token.getUser());
|
||||||
// TODO: redirect to frontend
|
response.sendRedirect(emailConfig.getRegistrationRedirect());
|
||||||
return new OkResponse();
|
|
||||||
} else {
|
} else {
|
||||||
throw new EmailTokenNotFoundException();
|
throw new EmailTokenNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,9 @@ spring.mail.properties.mail.smtp.writetimeout=5000
|
||||||
email.registrationSubject=Complete your SmartHut.sm registration
|
email.registrationSubject=Complete your SmartHut.sm registration
|
||||||
email.registration=To confirm your registration, please click here:
|
email.registration=To confirm your registration, please click here:
|
||||||
email.registrationPath=http://localhost:8080/register/confirm-account?token=
|
email.registrationPath=http://localhost:8080/register/confirm-account?token=
|
||||||
|
email.registrationRedirect=http://localhost:3000
|
||||||
|
|
||||||
email.resetpasswordSubject=SmartHut.sm password reset
|
email.resetpasswordSubject=SmartHut.sm password reset
|
||||||
email.resetpassword=To reset your password, please click here:
|
email.resetpassword=To reset your password, please click here:
|
||||||
email.resetpasswordPath=http://localhost:3000/password-reset?token=
|
email.resetpasswordPath=http://localhost:3000/password-reset?token=
|
||||||
|
email.resetPasswordRedirect=http://localhost:3000/conf-reset-pass
|
|
@ -32,8 +32,11 @@ spring.mail.properties.mail.smtp.writetimeout=5000
|
||||||
email.registrationSubject=Complete your SmartHut.sm registration
|
email.registrationSubject=Complete your SmartHut.sm registration
|
||||||
email.registration=To confirm your registration, please click here:
|
email.registration=To confirm your registration, please click here:
|
||||||
email.registrationPath=${BACKEND_URL}/register/confirm-account?token=
|
email.registrationPath=${BACKEND_URL}/register/confirm-account?token=
|
||||||
|
email.registrationSuccess=${FRONTEND_URL}
|
||||||
|
|
||||||
|
|
||||||
# Password reset email properties
|
# Password reset email properties
|
||||||
email.resetpasswordSubject=SmartHut.sm password reset
|
email.resetpasswordSubject=SmartHut.sm password reset
|
||||||
email.resetpassword=To reset your password, please click here:
|
email.resetpassword=To reset your password, please click here:
|
||||||
email.resetpasswordPath=${FRONTEND_URL}/password-reset?token=
|
email.resetpasswordPath=${FRONTEND_URL}/password-reset?token=
|
||||||
|
email.resetPasswordSuccess=${FRONTEND_URL}/conf-reset-pass
|
Loading…
Reference in a new issue