Skip to content

Commit 05ee114

Browse files
Fix actual link within the email confirmation link
1 parent d095dec commit 05ee114

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/main/java/com/accolite/pru/health/AuthApp/controller/AuthController.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.web.bind.annotation.RestController;
2626
import org.springframework.web.context.request.WebRequest;
2727
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
28+
import org.springframework.web.util.UriComponentsBuilder;
2829

2930
import javax.validation.Valid;
3031
import java.net.URI;
@@ -71,14 +72,16 @@ public ResponseEntity<?> registerUser(@Valid @RequestBody RegistrationRequest re
7172
registeredUserOpt.orElseThrow(() -> new UserRegistrationException("Couldn't register user [" + registrationRequest +
7273
"]"));
7374
User registeredUser = registeredUserOpt.get();
75+
UriComponentsBuilder urlBuilder = ServletUriComponentsBuilder.fromCurrentContextPath().path("/api/auth" +
76+
"/registrationConfirmation");
77+
7478
OnUserRegistrationCompleteEvent onUserRegistrationCompleteEvent =
75-
new OnUserRegistrationCompleteEvent(registeredUser, request.getContextPath() +
76-
"/registrationConfirmation");
79+
new OnUserRegistrationCompleteEvent(registeredUser, urlBuilder);
7780
applicationEventPublisher.publishEvent(onUserRegistrationCompleteEvent);
7881

7982
logger.info("Registered User returned [API[: " + registeredUser);
8083
URI location = ServletUriComponentsBuilder
81-
.fromCurrentContextPath().path("/api/users/{email}")
84+
.fromCurrentContextPath().path("/api/user/me")
8285
.buildAndExpand(registeredUser.getEmail()).toUri();
8386

8487
return ResponseEntity.created(location).body(new ApiResponse("User registered successfully", true));
@@ -93,7 +96,7 @@ public ResponseEntity<?> confirmRegistration(@RequestParam("token") String token
9396

9497
User verifiedUser = verifiedUserOpt.get();
9598
URI location = ServletUriComponentsBuilder
96-
.fromCurrentContextPath().path("/api/users/{email}")
99+
.fromCurrentContextPath().path("/api/user/me")
97100
.buildAndExpand(verifiedUser.getEmail()).toUri();
98101
return ResponseEntity.created(location).body(new ApiResponse("User verified successfully", true));
99102
}

src/main/java/com/accolite/pru/health/AuthApp/controller/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public ResponseEntity<?> checkUsernameInUse(@RequestParam("username") String use
4141
}
4242

4343
@GetMapping("/me")
44-
public ResponseEntity<?> getUserProfile(@CurrentUser CustomUserDetails customUserDetails) {
44+
public ResponseEntity<?> getUserProfile(@CurrentUser CustomUserDetails currentUser) {
4545
logger.info("Inside secured resource with user");
46-
logger.info(customUserDetails.getEmail() + " has role: " + customUserDetails.getRoles());
46+
logger.info(currentUser.getEmail() + " has role: " + currentUser.getRoles());
4747
return ResponseEntity.ok("Hello. This is about me");
4848
}
4949

src/main/java/com/accolite/pru/health/AuthApp/event/OnUserRegistrationCompleteEvent.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
import com.accolite.pru.health.AuthApp.model.User;
44
import org.springframework.context.ApplicationEvent;
5+
import org.springframework.web.util.UriComponentsBuilder;
56

67
public class OnUserRegistrationCompleteEvent extends ApplicationEvent {
78

8-
private String redirectUrl;
9+
private UriComponentsBuilder redirectUrl;
910
private User user;
1011

1112
public OnUserRegistrationCompleteEvent(
12-
User user, String redirectUrl) {
13+
User user, UriComponentsBuilder redirectUrl) {
1314
super(user);
1415
this.user = user;
1516
this.redirectUrl = redirectUrl;
1617
}
1718

18-
public String getRedirectUrl() {
19+
public UriComponentsBuilder getRedirectUrl() {
1920
return redirectUrl;
2021
}
2122

22-
public void setRedirectUrl(String redirectUrl) {
23+
public void setRedirectUrl(UriComponentsBuilder redirectUrl) {
2324
this.redirectUrl = redirectUrl;
2425
}
2526

src/main/java/com/accolite/pru/health/AuthApp/event/listener/OnUserRegistrationCompleteListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void sendEmailVerification(OnUserRegistrationCompleteEvent event) {
4343
emailVerificationTokenService.createVerificationToken(user, token);
4444

4545
String recipientAddress = user.getEmail();
46-
String emailConfirmationUrl = event.getRedirectUrl() + token;
46+
String emailConfirmationUrl = event.getRedirectUrl().queryParam("token", token).toUriString();
4747
try {
4848
mailService.sendEmailVerification(emailConfirmationUrl, recipientAddress);
4949
} catch (IOException | TemplateException | MessagingException e) {

0 commit comments

Comments
 (0)