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

@@ -164,6 +164,11 @@
<artifactId>libphonenumber</artifactId>
<version>8.13.36</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>3.1.5</version>
</dependency>
</dependencies>

View File

@@ -29,7 +29,8 @@ public class SecurityConfig {
http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/explorer/**").permitAll()
.requestMatchers("/test/**").permitAll()
.requestMatchers("/explorer/**").permitAll()
.requestMatchers("/nflow/**").permitAll()
.requestMatchers("/auth/**").permitAll()
.requestMatchers("/public/**").permitAll()

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

View File

@@ -59,3 +59,9 @@ nflow.db.postgresql.password=zdDZMzq6F4B4L1IUl
infobip.url=https://z3m696.api.infobip.com/sms/2/text/advanced
infobip.apikey=0ede918b7c1110465a0518021521638c-ebc4fdb7-547d-413b-bbca-781949ac6597
spring.mail.host=mail.zimscape.com
spring.mail.port=587
spring.mail.username=notifications@qantra.co.zw
spring.mail.password=QdZucpJMJIZ
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true