Files
backend/account-service/src/main/java/com/drinkool/services/OtpService.java
T
TakahashiNguyen a2b9de91f5
Release package / release (push) Failing after 2m57s
feat: init customer authentication (#2)
Co-authored-by: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com>
Co-authored-by: gitea-actions <actions-user@noreply.git.demonkernel.io.vn>
Reviewed-on: #2
2026-03-23 13:21:00 +00:00

40 lines
927 B
Java

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