Files
backend/account-service/src/main/java/com/drinkool/services/AuthenticationService.java
T
TakahashiNguyen 837f5f595a
Release package / release (push) Failing after 2m10s
feat: init k8s and customer service (#15)
Co-authored-by: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com>
Reviewed-on: #15
2026-04-08 08:24:00 +00:00

111 lines
2.6 KiB
Java

package com.drinkool.services;
import com.drinkool.Jwt;
import com.drinkool.Role;
import com.drinkool.Url;
import com.drinkool.dtos.*;
import com.drinkool.entities.CustomerEntity;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
@Path("")
public class AuthenticationService {
@Inject
OtpService otpService;
@POST
@Path(Url.Signup)
@Transactional
public Response signup(Signup input) {
CustomerEntity newCustomer = new CustomerEntity();
newCustomer.signup(input.name, input.phone, input.password);
return Response.status(Response.Status.CREATED)
.header(Jwt.userId, newCustomer.id.toString())
.header(Jwt.role, Role.Customer)
.build();
}
@POST
@Path(Url.QuickSignup)
@Transactional
public Response quickSignup(QuickSignup input) {
CustomerEntity newCustomer = new CustomerEntity();
newCustomer.signup(input.phone);
return Response.status(Response.Status.CREATED)
.header(Jwt.userId, newCustomer.id.toString())
.header(Jwt.role, Role.Customer)
.build();
}
@POST
@Path(Url.Login)
public Response login(Login input) {
CustomerEntity customer = CustomerEntity.find(
"phone",
input.phone
).firstResult();
if (customer == null) return Response.status(Response.Status.BAD_REQUEST)
.entity("InvalidPhoneNumber")
.build();
if (!customer.login(input.password)) return Response.status(
Response.Status.BAD_REQUEST
)
.entity("InvalidPassword")
.build();
return Response.accepted()
.header(Jwt.userId, customer.id.toString())
.header(Jwt.role, Role.Customer)
.build();
}
@POST
@Path(Url.QuickLogin)
public Response quickLogin(QuickLogin input) {
if (!otpService.verifyOtp(input.phone, input.otp)) {
return Response.status(Response.Status.UNAUTHORIZED)
.entity("InvalidOTP")
.build();
}
CustomerEntity customer = CustomerEntity.find(
"phone",
input.phone
).firstResult();
return Response.accepted()
.header(Jwt.userId, customer.id.toString())
.header(Jwt.role, Role.Customer)
.build();
}
@POST
@Path(Url.SmsOtp)
public Response smsOtp(SmsOtp input) {
CustomerEntity customer = CustomerEntity.find(
"phone",
input.phone
).firstResult();
if (customer == null) return Response.status(Response.Status.BAD_REQUEST)
.entity("InvalidPhoneNumber")
.build();
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();
}
}