-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AYS-318 | Forgot Password Flow Has Been Created (#344)
- Loading branch information
1 parent
f0e0170
commit f08ed0e
Showing
22 changed files
with
584 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/org/ays/auth/model/request/AysForgotPasswordRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.ays.auth.model.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.ays.common.util.validation.Email; | ||
|
||
@Getter | ||
@Setter | ||
public class AysForgotPasswordRequest { | ||
|
||
@NotBlank | ||
@Size(min = 2, max = 255) | ||
private String emailAddress; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/org/ays/auth/service/AysUserPasswordService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.ays.auth.service; | ||
|
||
import org.ays.auth.model.request.AysForgotPasswordRequest; | ||
|
||
/** | ||
* Service interface for handling user password operations. | ||
* Implementations of this interface should provide functionality for handling forgotten passwords. | ||
*/ | ||
public interface AysUserPasswordService { | ||
|
||
/** | ||
* Handles the forgot password request by sending an email to the user | ||
* with instructions to create a new password. | ||
* | ||
* @param forgotPasswordRequest the request containing the user's email address. | ||
*/ | ||
void forgotPassword(AysForgotPasswordRequest forgotPasswordRequest); | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/org/ays/auth/service/impl/AysUserPasswordServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.ays.auth.service.impl; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.ays.auth.model.AysUser; | ||
import org.ays.auth.model.enums.AysConfigurationParameter; | ||
import org.ays.auth.model.request.AysForgotPasswordRequest; | ||
import org.ays.auth.port.AysUserReadPort; | ||
import org.ays.auth.port.AysUserSavePort; | ||
import org.ays.auth.service.AysUserPasswordService; | ||
import org.ays.auth.util.exception.AysEmailAddressNotValidException; | ||
import org.ays.common.model.AysMail; | ||
import org.ays.common.model.enums.AysMailTemplate; | ||
import org.ays.common.service.AysMailService; | ||
import org.ays.common.util.AysRandomUtil; | ||
import org.ays.parameter.model.AysParameter; | ||
import org.ays.parameter.port.AysParameterReadPort; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Service implementation for handling user password operations such as forgotten password. | ||
* This service handles the retrieval of user information and sending emails for password creation. | ||
*/ | ||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
class AysUserPasswordServiceImpl implements AysUserPasswordService { | ||
|
||
private final AysUserReadPort userReadPort; | ||
private final AysUserSavePort userSavePort; | ||
private final AysMailService mailService; | ||
private final AysParameterReadPort parameterReadPort; | ||
|
||
/** | ||
* Handles the forgot password request by sending an email to the user | ||
* with instructions to create a new password. | ||
* | ||
* @param forgotPasswordRequest the request containing the user's email address. | ||
* @throws AysEmailAddressNotValidException if the email address is not associated with any user. | ||
*/ | ||
@Override | ||
public void forgotPassword(final AysForgotPasswordRequest forgotPasswordRequest) { | ||
|
||
final String emailAddress = forgotPasswordRequest.getEmailAddress(); | ||
final AysUser user = userReadPort.findByEmailAddress(emailAddress) | ||
.orElseThrow(() -> new AysEmailAddressNotValidException(emailAddress)); | ||
|
||
if (user.getPassword() != null) { | ||
this.sendPasswordCreateEmail(user); | ||
return; | ||
} | ||
|
||
final AysUser.Password password = AysUser.Password.builder() | ||
.value(AysRandomUtil.generateUUID()) | ||
.build(); | ||
user.setPassword(password); | ||
AysUser savedUser = userSavePort.save(user); | ||
|
||
this.sendPasswordCreateEmail(savedUser); | ||
} | ||
|
||
/** | ||
* Sends an email to the user with instructions to create a new password. | ||
* | ||
* @param user the user to whom the email should be sent. | ||
*/ | ||
private void sendPasswordCreateEmail(final AysUser user) { | ||
|
||
final Map<String, Object> parameters = Map.of( | ||
"userFullName", user.getFirstName() + " " + user.getLastName(), | ||
"url", this.findFeUrl().concat("/create-password/").concat(user.getPassword().getId()) | ||
); | ||
|
||
final AysMail mail = AysMail.builder() | ||
.to(List.of(user.getEmailAddress())) | ||
.template(AysMailTemplate.CREATE_PASSWORD) | ||
.parameters(parameters) | ||
.build(); | ||
|
||
mailService.send(mail); | ||
} | ||
|
||
/** | ||
* Retrieves the Front-End URL from the configuration parameters. | ||
* | ||
* @return the Front-End URL. | ||
*/ | ||
private String findFeUrl() { | ||
return parameterReadPort | ||
.findByName(AysConfigurationParameter.FE_URL.name()) | ||
.orElse(AysParameter.from(AysConfigurationParameter.FE_URL)) | ||
.getDefinition(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import jakarta.mail.Message; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.InternetAddress; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
@@ -66,6 +67,8 @@ private MimeMessage createMimeMessage(final AysMail mail) throws IOException, Me | |
String htmlContentWithParameters = this.addParameters(htmlContent, mail.getParameters()); | ||
mimeMessage.setText(htmlContentWithParameters, "UTF-8", "html"); | ||
|
||
mimeMessage.setFrom(new InternetAddress("[email protected]", "Afet Yönetim Sistemi")); | ||
|
||
for (String to : mail.getTo()) { | ||
mimeMessage.addRecipients(Message.RecipientType.TO, to); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.