|
|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
}
|