adding email otp verification
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -164,6 +164,11 @@
|
|||||||
<artifactId>libphonenumber</artifactId>
|
<artifactId>libphonenumber</artifactId>
|
||||||
<version>8.13.36</version> <!-- Use the latest version -->
|
<version>8.13.36</version> <!-- Use the latest version -->
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
|
<version>3.1.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ public class SecurityConfig {
|
|||||||
http
|
http
|
||||||
.csrf(AbstractHttpConfigurer::disable)
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("/explorer/**").permitAll()
|
.requestMatchers("/test/**").permitAll()
|
||||||
|
.requestMatchers("/explorer/**").permitAll()
|
||||||
.requestMatchers("/nflow/**").permitAll()
|
.requestMatchers("/nflow/**").permitAll()
|
||||||
.requestMatchers("/auth/**").permitAll()
|
.requestMatchers("/auth/**").permitAll()
|
||||||
.requestMatchers("/public/**").permitAll()
|
.requestMatchers("/public/**").permitAll()
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
package zw.qantra.tm.domain.controllers;
|
package zw.qantra.tm.domain.controllers;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import zw.qantra.tm.domain.services.EmailService;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/test")
|
@RequestMapping("/test")
|
||||||
@CrossOrigin(origins = "*")
|
@CrossOrigin(origins = "*")
|
||||||
public class TestController {
|
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")
|
@GetMapping("/protected")
|
||||||
public ResponseEntity<String> protectedEndpoint() {
|
public ResponseEntity<String> protectedEndpoint() {
|
||||||
|
|||||||
23
src/main/java/zw/qantra/tm/domain/services/EmailService.java
Normal file
23
src/main/java/zw/qantra/tm/domain/services/EmailService.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ import zw.qantra.tm.domain.dtos.*;
|
|||||||
import zw.qantra.tm.domain.dtos.erp.CustomerDto;
|
import zw.qantra.tm.domain.dtos.erp.CustomerDto;
|
||||||
import zw.qantra.tm.domain.models.Otp;
|
import zw.qantra.tm.domain.models.Otp;
|
||||||
import zw.qantra.tm.domain.models.User;
|
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.OtpService;
|
||||||
import zw.qantra.tm.domain.services.UserService;
|
import zw.qantra.tm.domain.services.UserService;
|
||||||
import io.nflow.engine.workflow.curated.State;
|
import io.nflow.engine.workflow.curated.State;
|
||||||
@@ -46,6 +47,8 @@ public class RegistrationWorkflow extends WorkflowDefinition {
|
|||||||
private OtpService otpService;
|
private OtpService otpService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestService restService;
|
private RestService restService;
|
||||||
|
@Autowired
|
||||||
|
private EmailService emailService;
|
||||||
|
|
||||||
@Value("${infobip.url}")
|
@Value("${infobip.url}")
|
||||||
private String infobipUrl;
|
private String infobipUrl;
|
||||||
@@ -136,11 +139,8 @@ public class RegistrationWorkflow extends WorkflowDefinition {
|
|||||||
// Generate OTP
|
// Generate OTP
|
||||||
Otp otp = otpService.generateOtp(username, "REGISTRATION");
|
Otp otp = otpService.generateOtp(username, "REGISTRATION");
|
||||||
|
|
||||||
String string = getSMSString(registerRequest.getPhone(), otp.getOtp());
|
String string = String.format("Your Peak verification code is: %s", otp.getOtp());
|
||||||
HttpHeaders headers = new HttpHeaders();
|
emailService.sendSimpleMessage(email, "Peak verification code", string);
|
||||||
headers.add("Authorization", "App " + infobipApiKey);
|
|
||||||
headers.add("Content-Type", "application/json");
|
|
||||||
headers.add("Accept", "application/json");
|
|
||||||
// LinkedHashMap response = restService.postAs(infobipUrl, string, headers, LinkedHashMap.class);
|
// LinkedHashMap response = restService.postAs(infobipUrl, string, headers, LinkedHashMap.class);
|
||||||
|
|
||||||
log.info("otp generated successfully: {}", otp.getOtp());
|
log.info("otp generated successfully: {}", otp.getOtp());
|
||||||
@@ -164,7 +164,8 @@ public class RegistrationWorkflow extends WorkflowDefinition {
|
|||||||
public void resendOtp(StateExecution execution) {
|
public void resendOtp(StateExecution execution) {
|
||||||
OtpRequest otpRequest = execution.getVariable("body", OtpRequest.class);
|
OtpRequest otpRequest = execution.getVariable("body", OtpRequest.class);
|
||||||
Otp otp = otpService.generateOtp(otpRequest.getUsername(), "REGISTRATION");
|
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());
|
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());
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,3 +59,9 @@ nflow.db.postgresql.password=zdDZMzq6F4B4L1IUl
|
|||||||
infobip.url=https://z3m696.api.infobip.com/sms/2/text/advanced
|
infobip.url=https://z3m696.api.infobip.com/sms/2/text/advanced
|
||||||
infobip.apikey=0ede918b7c1110465a0518021521638c-ebc4fdb7-547d-413b-bbca-781949ac6597
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user