adding email otp verification

This commit is contained in:
2025-10-09 20:33:12 +02:00
parent 230f16e08d
commit f6e04f302c
6 changed files with 54 additions and 27 deletions

View File

@@ -1,17 +1,24 @@
package zw.qantra.tm.domain.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import zw.qantra.tm.domain.services.EmailService;
@RestController
@RequestMapping("/test")
@CrossOrigin(origins = "*")
public class TestController {
@Autowired
private EmailService emailService;
@GetMapping("/hello/{email}")
public ResponseEntity<String> helloEndpoint(@PathVariable String email) {
emailService.sendSimpleMessage(email, "Hello", "Hello World!");
return ResponseEntity.ok("Hello World!");
}
@GetMapping("/protected")
public ResponseEntity<String> protectedEndpoint() {

View File

@@ -0,0 +1,23 @@
package zw.qantra.tm.domain.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
public class EmailService {
@Autowired
private JavaMailSender emailSender;
public void sendSimpleMessage(
String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("noreply@qantra.co.zw");
message.setTo(to);
message.setSubject(subject);
message.setText(text);
emailSender.send(message);
}
}

View File

@@ -18,6 +18,7 @@ import zw.qantra.tm.domain.dtos.*;
import zw.qantra.tm.domain.dtos.erp.CustomerDto;
import zw.qantra.tm.domain.models.Otp;
import zw.qantra.tm.domain.models.User;
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;
@@ -46,6 +47,8 @@ public class RegistrationWorkflow extends WorkflowDefinition {
private OtpService otpService;
@Autowired
private RestService restService;
@Autowired
private EmailService emailService;
@Value("${infobip.url}")
private String infobipUrl;
@@ -136,11 +139,8 @@ public class RegistrationWorkflow extends WorkflowDefinition {
// Generate OTP
Otp otp = otpService.generateOtp(username, "REGISTRATION");
String string = getSMSString(registerRequest.getPhone(), otp.getOtp());
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "App " + infobipApiKey);
headers.add("Content-Type", "application/json");
headers.add("Accept", "application/json");
String string = String.format("Your Peak verification code is: %s", otp.getOtp());
emailService.sendSimpleMessage(email, "Peak verification code", string);
// LinkedHashMap response = restService.postAs(infobipUrl, string, headers, LinkedHashMap.class);
log.info("otp generated successfully: {}", otp.getOtp());
@@ -164,7 +164,8 @@ public class RegistrationWorkflow extends WorkflowDefinition {
public void resendOtp(StateExecution execution) {
OtpRequest otpRequest = execution.getVariable("body", OtpRequest.class);
Otp otp = otpService.generateOtp(otpRequest.getUsername(), "REGISTRATION");
String string = getSMSString(otpRequest.getPhone(), otp.getOtp());
String string = String.format("Your Peak verification code is: %s", otp.getOtp());
emailService.sendSimpleMessage(otpRequest.getUsername(), "Peak verification code", string);
log.info("otp generated successfully: {}", otp.getOtp());
}
@@ -249,20 +250,4 @@ public class RegistrationWorkflow extends WorkflowDefinition {
return NextAction.stopInState(DONE, "ERP updated successfully for: " + registerRequest.getUsername());
}
String getSMSString(String to, String otp) {
return String.format("{\n" +
" \"messages\": [\n" +
" {\n" +
" \"destinations\": [\n" +
" {\n" +
" \"to\": \"%s\"\n" +
" }\n" +
" ],\n" +
" \"from\": \"447491163443\",\n" +
" \"text\": \"Your verification code is: %s\"\n" +
" }\n" +
" ]\n" +
"}", to, otp);
}
}