otpOpt = otpRepository.findByUsernameAndOtpType(username, otpType);
+ return otpOpt.isPresent() && otpOpt.get().getOtpStatus() == Status.ACTIVE;
+ }
+
+ private String generateRandomOtp() {
+ StringBuilder otp = new StringBuilder(OTP_LENGTH);
+ for (int i = 0; i < OTP_LENGTH; i++) {
+ otp.append(NUMBERS.charAt(random.nextInt(NUMBERS.length())));
+ }
+ return otp.toString();
+ }
+}
diff --git a/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java b/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java
index 41f6910..f6ee5e9 100644
--- a/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java
+++ b/src/main/java/zw/qantra/tm/domain/workflows/RegistrationWorkflow.java
@@ -1,12 +1,17 @@
package zw.qantra.tm.domain.workflows;
import io.nflow.engine.workflow.definition.WorkflowStateType;
+import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import io.nflow.engine.workflow.definition.NextAction;
import io.nflow.engine.workflow.definition.StateExecution;
import io.nflow.engine.workflow.definition.WorkflowDefinition;
+import zw.qantra.tm.domain.dtos.EmailRequest;
+import zw.qantra.tm.domain.models.Otp;
+import zw.qantra.tm.domain.services.EmailService;
+import zw.qantra.tm.domain.services.OtpService;
import zw.qantra.tm.domain.services.UserService;
import io.nflow.engine.workflow.curated.State;
import zw.qantra.tm.domain.dtos.AuthResponse;
@@ -18,6 +23,7 @@ import java.util.Set;
@Component
public class RegistrationWorkflow extends WorkflowDefinition {
+ Logger log = org.slf4j.LoggerFactory.getLogger(RegistrationWorkflow.class);
public static final String TYPE = "registrationWorkflow";
@@ -35,6 +41,10 @@ public class RegistrationWorkflow extends WorkflowDefinition {
private UserService userService;
@Autowired
private Validator validator;
+ @Autowired
+ private OtpService otpService;
+ @Autowired
+ private EmailService emailService;
public RegistrationWorkflow() {
super(TYPE, BEGIN, FAILED);
@@ -72,14 +82,50 @@ public class RegistrationWorkflow extends WorkflowDefinition {
}
// todo: add this to the appropriate action
- // AuthResponse response = userService.register(payload);
execution.setVariable("metadata", payload);
return NextAction.moveToState(SEND_OTP, "Initial registration success for: " + payload.getUsername());
}
public void sendOtp(StateExecution execution) {
- RegisterRequest response = execution.getVariable("metadata", RegisterRequest.class);
+ try {
+ RegisterRequest registerRequest = execution.getVariable("metadata", RegisterRequest.class);
+ String username = registerRequest.getUsername();
+ String email = registerRequest.getEmail();
+
+ // Generate OTP
+ Otp otp = otpService.generateOtp(username, "REGISTRATION");
+
+ // Create email content
+ String subject = "Your Verification Code";
+ String body = String.format(
+ "" +
+ "Email Verification
" +
+ "Hello %s,
" +
+ "Your verification code is: %s
" +
+ "This code will expire in 10 minutes.
" +
+ "If you didn't request this code, please ignore this email.
" +
+ "",
+ username, otp.getOtp()
+ );
+
+ // Send email
+ EmailRequest emailRequest = EmailRequest.builder()
+ .to(email)
+ .subject(subject)
+ .body(body)
+ .isHtml(true)
+ .build();
+
+ boolean emailSent = emailService.sendEmail(emailRequest);
+
+ if (!emailSent) {
+ log.error("Failed to send OTP email to: {}", email);
+ }
+
+ } catch (Exception e) {
+ log.error("Error sending OTP: {}", e.getMessage(), e);
+ }
}
public NextAction verifyOtp(StateExecution execution) {
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index bf9264a..0799893 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -47,3 +47,15 @@ jwt.secret=your-super-secret-jwt-key-here-make-it-very-long-and-secure-in-produc
jwt.expiration=86400000
logging.level.root=WARN
+
+# Email Configuration
+spring.mail.host=smtp.gmail.com
+spring.mail.port=587
+spring.mail.username=vusakhoza@gmail.com
+spring.mail.password=Stikbanch@14
+spring.mail.properties.mail.smtp.auth=true
+spring.mail.properties.mail.smtp.starttls.enable=true
+spring.mail.properties.mail.smtp.starttls.required=true
+spring.mail.properties.mail.smtp.connectiontimeout=5000
+spring.mail.properties.mail.smtp.timeout=5000
+spring.mail.properties.mail.smtp.writetimeout=5000