feat: init customer authentication #2
+10
-15
@@ -1,9 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
@@ -20,11 +16,9 @@
|
||||
<compiler-plugin.version>3.15.0</compiler-plugin.version>
|
||||
<maven.compiler.release>21</maven.compiler.release>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding
|
||||
>UTF-8</project.reporting.outputEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
|
||||
<quarkus.platform.group-id
|
||||
>io.quarkus.platform</quarkus.platform.group-id>
|
||||
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
|
||||
<quarkus.platform.version>3.32.4</quarkus.platform.version>
|
||||
<skipITs>true</skipITs>
|
||||
<surefire-plugin.version>3.5.4</surefire-plugin.version>
|
||||
@@ -76,6 +70,10 @@
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-container-image-docker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-redis-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit</artifactId>
|
||||
@@ -119,8 +117,7 @@
|
||||
<configuration>
|
||||
<argLine>@{argLine}</argLine>
|
||||
<systemPropertyVariables>
|
||||
<java.util.logging.manager
|
||||
>org.jboss.logmanager.LogManager</java.util.logging.manager>
|
||||
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
|
||||
<maven.home>${maven.home}</maven.home>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
@@ -139,10 +136,8 @@
|
||||
<configuration>
|
||||
<argLine>@{argLine}</argLine>
|
||||
<systemPropertyVariables>
|
||||
<native.image.path>
|
||||
${project.build.directory}/${project.build.finalName}-runner</native.image.path>
|
||||
<java.util.logging.manager
|
||||
>org.jboss.logmanager.LogManager</java.util.logging.manager>
|
||||
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
|
||||
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
|
||||
<maven.home>${maven.home}</maven.home>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.drinkool;
|
||||
|
||||
import com.drinkool.dtos.*;
|
||||
import com.drinkool.models.Customer;
|
||||
import com.drinkool.services.*;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.transaction.Transactional;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.*;
|
||||
@@ -9,6 +11,9 @@ import jakarta.ws.rs.core.*;
|
||||
@Path("/api")
|
||||
public class RestAPI {
|
||||
|
||||
@Inject
|
||||
OtpService otpService;
|
||||
|
||||
@POST
|
||||
@Path("/signup")
|
||||
@Transactional
|
||||
@@ -44,4 +49,26 @@ public class RestAPI {
|
||||
|
||||
return Response.accepted().build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/sms_otp")
|
||||
public Response smsOtp(SmsOtp input) {
|
||||
String otp = otpService.generateAndSaveOtp(input.phone);
|
||||
|
||||
// Ở đây bạn sẽ gọi Kafka để gửi SMS thực tế
|
||||
System.out.println("OTP cho " + input.phone + " là: " + otp);
|
||||
|
||||
return Response.accepted().build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/quicklogin")
|
||||
public Response quickLogin(QuickLogin input) {
|
||||
if (otpService.verifyOtp(input.phone, input.otp)) {
|
||||
return Response.accepted().build();
|
||||
}
|
||||
return Response.status(Response.Status.UNAUTHORIZED)
|
||||
.entity("InvalidOTP")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.drinkool.services;
|
||||
|
||||
import io.quarkus.redis.datasource.RedisDataSource;
|
||||
import io.quarkus.redis.datasource.value.ValueCommands;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import java.util.Random;
|
||||
|
||||
@ApplicationScoped
|
||||
public class OtpService {
|
||||
|
||||
private final ValueCommands<String, String> countCommands;
|
||||
|
||||
@Inject
|
||||
public OtpService(RedisDataSource ds) {
|
||||
this.countCommands = ds.value(String.class);
|
||||
}
|
||||
|
||||
public String generateAndSaveOtp(String phone) {
|
||||
String otp = String.format("%06d", new Random().nextInt(1000000));
|
||||
String key = "otp:" + phone;
|
||||
|
||||
countCommands.setex(key, 300, otp);
|
||||
|
||||
return otp;
|
||||
}
|
||||
|
||||
public boolean verifyOtp(String phone, String inputOtp) {
|
||||
String key = "otp:" + phone;
|
||||
String savedOtp = countCommands.get(key);
|
||||
|
||||
if (savedOtp != null && savedOtp.equals(inputOtp)) {
|
||||
countCommands.getdel(key);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -14,3 +14,6 @@ quarkus.container-image.name=account-service
|
||||
quarkus.container-image.push=true
|
||||
quarkus.container-image.build=true
|
||||
quarkus.container-image.builder=docker
|
||||
|
||||
# Redis
|
||||
quarkus.redis.hosts=redis://localhost:6379
|
||||
@@ -52,10 +52,6 @@
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-redis-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-messaging-kafka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-rest-jackson</artifactId>
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.drinkool;
|
||||
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import java.util.stream.Stream;
|
||||
import org.eclipse.microprofile.reactive.messaging.Channel;
|
||||
import org.eclipse.microprofile.reactive.messaging.Emitter;
|
||||
import org.eclipse.microprofile.reactive.messaging.Incoming;
|
||||
import org.eclipse.microprofile.reactive.messaging.Outgoing;
|
||||
|
||||
@ApplicationScoped
|
||||
public class MyMessagingApplication {
|
||||
|
||||
/**
|
||||
* Injects an emitter to send messages to the "words-out" channel.
|
||||
*/
|
||||
@Channel("words-out")
|
||||
Emitter<String> emitter;
|
||||
|
||||
/**
|
||||
* Sends message to the "words-out" channel, can be used from a JAX-RS resource or any bean of your application.
|
||||
* Messages are sent to the broker.
|
||||
**/
|
||||
void onStart(@Observes StartupEvent ev) {
|
||||
Stream.of("Hello", "with", "Quarkus", "Messaging", "message").forEach(
|
||||
string -> emitter.send(string)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume the message from the "words-in" channel, uppercase it and send it to the uppercase channel.
|
||||
* This method is called by the framework when a message is received on the "words-in" channel (from the broker).
|
||||
**/
|
||||
@Incoming("words-in")
|
||||
@Outgoing("uppercase")
|
||||
public String toUpperCase(String message) {
|
||||
return message.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume the uppercase channel (coming from within the application) and print the messages to the console.
|
||||
**/
|
||||
@Incoming("uppercase")
|
||||
public void sink(String word) {
|
||||
System.out.println(">> " + word);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.drinkool.dtos;
|
||||
|
||||
public class QuickLogin {
|
||||
|
||||
public String phone;
|
||||
public String otp;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.drinkool.dtos;
|
||||
|
||||
public class SmsOtp {
|
||||
public String phone;
|
||||
}
|
||||
Reference in New Issue
Block a user