-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added mail server and implemented whats needed for password recovery
- Loading branch information
1 parent
4437ad1
commit d8d3284
Showing
15 changed files
with
345 additions
and
50 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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/example/usermanagement/entities/PasswordResetToken.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,52 @@ | ||
package com.example.usermanagement.entities; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@Table(name = "password_reset_tokens") | ||
@NoArgsConstructor | ||
public class PasswordResetToken { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
private UUID id; | ||
|
||
@Column(nullable = false, unique = true) | ||
private String token; | ||
|
||
@Column(nullable = false, name = "expiry_date") | ||
private Instant expiryDate; | ||
|
||
@Column(nullable = false, name = "created_date") | ||
private Instant createdDate; | ||
|
||
@Column(nullable = false, name = "is_used") | ||
private Boolean isUsed; | ||
|
||
@OneToOne(fetch = FetchType.LAZY) | ||
private Account account; | ||
|
||
public PasswordResetToken(Account account, int expiryTimeInSec) { | ||
this.token = UUID.randomUUID().toString(); | ||
this.createdDate = Instant.now(); | ||
this.expiryDate = this.createdDate.plusSeconds(expiryTimeInSec); | ||
this.isUsed = false; | ||
this.account = account; | ||
} | ||
|
||
public boolean isExpired() { | ||
return Instant.now().isAfter(this.expiryDate); | ||
} | ||
|
||
public boolean haveBeenCreatedLately(int seconds) { | ||
return Instant.now().isBefore(this.createdDate.plusSeconds(seconds)); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/example/usermanagement/events/listeners/EmailsListener.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,43 @@ | ||
package com.example.usermanagement.events.listeners; | ||
|
||
|
||
import com.example.usermanagement.events.publishers.emails.EmailVerificationTokenGeneratedEvent; | ||
import com.example.usermanagement.events.publishers.emails.PasswordHaveBeenResetedEvent; | ||
import com.example.usermanagement.events.publishers.emails.PasswordResetGeneratedEvent; | ||
import com.example.usermanagement.interfaces.services.IEmailService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
// TODO: make it use thymeleaf for email templates | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class EmailsListener { | ||
|
||
@Value("${app.clients.web.amwc}") | ||
private String amwc; | ||
|
||
private final IEmailService emailService; | ||
|
||
@EventListener(EmailVerificationTokenGeneratedEvent.class) | ||
public void onEmailVerificationTokenGeneratedEvent(EmailVerificationTokenGeneratedEvent event) { | ||
String link = amwc + "/api/accounts/verify-email?token=" + event.getToken(); | ||
String body = "Click here to verify your email: " + link; | ||
emailService.sendEmail("wa55death405@gmail.com", "Email verification", body); | ||
} | ||
|
||
@EventListener(PasswordResetGeneratedEvent.class) | ||
public void onPasswordResetGeneratedEvent(PasswordResetGeneratedEvent event) { | ||
String link = amwc + "/api/accounts/reset-password?token=" + event.getToken(); | ||
String body = "Click here to reset your password: " + link; | ||
emailService.sendEmail(event.getEmail(), "Password reset", body); | ||
} | ||
|
||
@EventListener(PasswordHaveBeenResetedEvent.class) | ||
public void onPasswordHaveBeenResetedEvent(PasswordHaveBeenResetedEvent event) { | ||
String body = "Your password have been reseted"; | ||
emailService.sendEmail(event.getEmail(), "Password reseted", body); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...example/usermanagement/events/publishers/emails/EmailVerificationTokenGeneratedEvent.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,16 @@ | ||
package com.example.usermanagement.events.publishers.emails; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class EmailVerificationTokenGeneratedEvent extends ApplicationEvent{ | ||
private final String token; | ||
private final String email; | ||
|
||
public EmailVerificationTokenGeneratedEvent(Object source, String token, String email) { | ||
super(source); | ||
this.token = token; | ||
this.email = email; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ava/com/example/usermanagement/events/publishers/emails/PasswordHaveBeenResetedEvent.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,14 @@ | ||
package com.example.usermanagement.events.publishers.emails; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class PasswordHaveBeenResetedEvent extends ApplicationEvent { | ||
private final String email; | ||
|
||
public PasswordHaveBeenResetedEvent(Object source, String email) { | ||
super(source); | ||
this.email = email; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...java/com/example/usermanagement/events/publishers/emails/PasswordResetGeneratedEvent.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,16 @@ | ||
package com.example.usermanagement.events.publishers.emails; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class PasswordResetGeneratedEvent extends ApplicationEvent { | ||
private final String email; | ||
private final String token; | ||
|
||
public PasswordResetGeneratedEvent(Object source, String email, String token) { | ||
super(source); | ||
this.email = email; | ||
this.token = token; | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/java/com/example/usermanagement/interfaces/services/IEmailService.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,5 @@ | ||
package com.example.usermanagement.interfaces.services; | ||
|
||
public interface IEmailService { | ||
void sendEmail(String to, String subject, String body); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/usermanagement/interfaces/services/IPasswordResetTokenService.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,8 @@ | ||
package com.example.usermanagement.interfaces.services; | ||
|
||
import com.example.usermanagement.entities.Account; | ||
|
||
public interface IPasswordResetTokenService { | ||
String generatePasswordResetToken(Account account); | ||
String consumePasswordResetToken(String token); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/usermanagement/repositories/PasswordResetTokenRepository.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,13 @@ | ||
package com.example.usermanagement.repositories; | ||
|
||
import com.example.usermanagement.entities.Account; | ||
import com.example.usermanagement.entities.PasswordResetToken; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface PasswordResetTokenRepository extends JpaRepository<PasswordResetToken, UUID> { | ||
Optional<PasswordResetToken> findByToken(String token); | ||
Optional<PasswordResetToken> findByAccount(Account account); | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/example/usermanagement/services/EmailService.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,23 @@ | ||
package com.example.usermanagement.services; | ||
|
||
import com.example.usermanagement.interfaces.services.IEmailService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class EmailService implements IEmailService { | ||
private final JavaMailSender mailSender; | ||
|
||
public void sendEmail(String to, String subject, String body) { | ||
SimpleMailMessage message = new SimpleMailMessage(); | ||
message.setTo(to); | ||
message.setSubject(subject); | ||
message.setText(body); | ||
message.setFrom("isetch-google-club@gmail.com"); | ||
mailSender.send(message); | ||
} | ||
} |
Oops, something went wrong.