Jelajahi Sumber

improve reset password email content

Daniel Bohry 3 minggu lalu
induk
melakukan
e26cd894f7

+ 1 - 1
src/main/java/com/danielbohry/authservice/api/AuthController.java

@@ -53,7 +53,7 @@ public class AuthController {
 
     @PostMapping("forgot-password")
     public ResponseEntity<Void> forgotPassword(@RequestParam String username) {
-        service.forgotPassword(username);
+        service.sendResetPasswordEmail(username);
         return ResponseEntity.ok().build();
     }
 

+ 21 - 4
src/main/java/com/danielbohry/authservice/service/auth/AuthService.java

@@ -4,7 +4,6 @@ import com.danielbohry.authservice.api.dto.AuthenticationRequest;
 import com.danielbohry.authservice.api.dto.AuthenticationResponse;
 import com.danielbohry.authservice.client.MailClient;
 import com.danielbohry.authservice.domain.ApplicationUser;
-import com.danielbohry.authservice.exceptions.NotFoundException;
 import com.danielbohry.authservice.service.user.UserService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Value;
@@ -75,14 +74,17 @@ public class AuthService implements UserDetailsService {
         return buildResponse(user, authentication);
     }
 
-    public void forgotPassword(String username) {
+    public void sendResetPasswordEmail(String username) {
         try {
             ApplicationUser user = service.findByUsername(username);
             Authentication systemAuth = jwtService.generateSystemToken();
             Authentication userAuth = jwtService.generateToken(user, 10);
 
-            if (user.getEmail() != null && user.getEmail().isEmpty())
-                mailClient.sendMail(user.getEmail(), "Password change requested", host + "/?reset-password&token=" + userAuth.token(), "Bearer " + systemAuth.token());
+            if (user.getEmail() != null && !user.getEmail().isEmpty()) {
+                String resetUrl = host + "/?reset-password&token=" + userAuth.token();
+                String emailContent = buildContent(user.getUsername(), resetUrl);
+                mailClient.sendMail(user.getEmail(), "Password Reset Request - Auth Service", emailContent, "Bearer " + systemAuth.token());
+            }
         } catch (Exception ignored) {
         }
     }
@@ -105,4 +107,19 @@ public class AuthService implements UserDetailsService {
                 .build();
     }
 
+    private String buildContent(String username, String resetUrl) {
+        return String.format("""
+            Hello %s,
+
+            You requested a password reset for your account.
+
+            Click here to reset your password: %s
+
+            This link expires in 10 minutes.
+            If you didn't request this, please ignore this email.
+
+            Auth Service Team
+            """, username, resetUrl);
+    }
+
 }

+ 2 - 2
src/test/java/com/danielbohry/authservice/api/AuthControllerUnitTest.java

@@ -219,7 +219,7 @@ class AuthControllerUnitTest {
     void shouldHandleForgotPasswordSuccessfully() {
         // given
         String username = "testuser";
-        doNothing().when(authService).forgotPassword(username);
+        doNothing().when(authService).sendResetPasswordEmail(username);
 
         // when
         ResponseEntity<Void> response = authController.forgotPassword(username);
@@ -227,6 +227,6 @@ class AuthControllerUnitTest {
         // then
         assertNotNull(response);
         assertEquals(HttpStatus.OK, response.getStatusCode());
-        verify(authService).forgotPassword(username);
+        verify(authService).sendResetPasswordEmail(username);
     }
 }