enh
This commit is contained in:
parent
1729b028d0
commit
df84e4a6c9
5 changed files with 41 additions and 16 deletions
|
|
@ -1,4 +1,6 @@
|
|||
|
||||
import type { KVNamespace } from '@cloudflare/workers-types'
|
||||
|
||||
// Old env loading (Node only)
|
||||
// export const appUrl = process.env.APP_URL as string;
|
||||
//
|
||||
|
|
@ -109,6 +111,11 @@ export const getRazorpaySecret = () => getRuntimeEnv().RAZORPAY_SECRET as string
|
|||
|
||||
export const getOtpSenderAuthToken = () => getRuntimeEnv().OTP_SENDER_AUTH_TOKEN as string
|
||||
|
||||
export const getOtpKvNamespace = () => {
|
||||
const env = getRuntimeEnv()
|
||||
return (env.freshyo_otp || env.freshyo_otp_dev) as KVNamespace | undefined
|
||||
}
|
||||
|
||||
export const getMinOrderValue = () => Number(getRuntimeEnv().MIN_ORDER_VALUE as string)
|
||||
|
||||
export const getDeliveryCharge = () => Number(getRuntimeEnv().DELIVERY_CHARGE as string)
|
||||
|
|
|
|||
|
|
@ -1,26 +1,37 @@
|
|||
import { ApiError } from '@/src/lib/api-error'
|
||||
import { getOtpSenderAuthToken } from '@/src/lib/env-exporter'
|
||||
import { getOtpSenderAuthToken, getOtpKvNamespace } from '@/src/lib/env-exporter'
|
||||
import type { KVNamespace } from '@cloudflare/workers-types'
|
||||
|
||||
const otpStore = new Map<string, string>();
|
||||
const OTP_TTL_SECONDS = 300
|
||||
|
||||
const otpKey = (phone: string) => `otp:${phone}`
|
||||
|
||||
const getKv = (): KVNamespace => {
|
||||
const kv = getOtpKvNamespace()
|
||||
if (!kv) {
|
||||
throw new ApiError('OTP service not configured', 500)
|
||||
}
|
||||
return kv
|
||||
}
|
||||
|
||||
const setOtpCreds = (phone: string, verificationId: string) => {
|
||||
otpStore.set(phone, verificationId);
|
||||
};
|
||||
return getKv().put(otpKey(phone), verificationId, { expirationTtl: OTP_TTL_SECONDS })
|
||||
}
|
||||
|
||||
export function getOtpCreds(mobile: string) {
|
||||
const authKey = otpStore.get(mobile);
|
||||
export async function getOtpCreds(mobile: string) {
|
||||
const authKey = await getKv().get(otpKey(mobile))
|
||||
|
||||
return authKey || null;
|
||||
}
|
||||
|
||||
const clearOtpCreds = (phone: string) => {
|
||||
return getKv().delete(otpKey(phone))
|
||||
}
|
||||
|
||||
export const sendOtp = async (phone: string) => {
|
||||
if (!phone) {
|
||||
throw new ApiError("Phone number is required", 400);
|
||||
}
|
||||
if (phone === '9676651496') {
|
||||
setOtpCreds(phone, 'DEV_BYPASS_VERIFICATION_ID');
|
||||
return { success: true, message: 'OTP sent successfully (dev bypass)' };
|
||||
}
|
||||
const reqUrl = `https://cpaas.messagecentral.com/verification/v3/send?countryCode=91&flowType=SMS&mobileNumber=${phone}&timeout=300`;
|
||||
const resp = await fetch(reqUrl, {
|
||||
headers: {
|
||||
|
|
@ -29,9 +40,10 @@ export const sendOtp = async (phone: string) => {
|
|||
method: "POST",
|
||||
});
|
||||
const data = await resp.json();
|
||||
|
||||
|
||||
console.log({data})
|
||||
if (data.message === "SUCCESS") {
|
||||
setOtpCreds(phone, data.data.verificationId);
|
||||
await setOtpCreds(phone, data.data.verificationId);
|
||||
return { success: true, message: "OTP sent successfully", verificationId: data.data.verificationId };
|
||||
}
|
||||
if (data.message === "REQUEST_ALREADY_EXISTS") {
|
||||
|
|
@ -42,9 +54,6 @@ export const sendOtp = async (phone: string) => {
|
|||
};
|
||||
|
||||
export async function verifyOtpUtil(mobile: string, otp: string, verifId: string):Promise<boolean> {
|
||||
if (mobile === '9676651496') {
|
||||
return otp === '1234';
|
||||
}
|
||||
const reqUrl = `https://cpaas.messagecentral.com/verification/v3/validateOtp?&verificationId=${verifId}&code=${otp}`;
|
||||
const resp = await fetch(reqUrl, {
|
||||
method: "GET",
|
||||
|
|
@ -56,6 +65,7 @@ export async function verifyOtpUtil(mobile: string, otp: string, verifId: string
|
|||
const rawData = await resp.json();
|
||||
if (rawData.data?.verificationStatus === "VERIFICATION_COMPLETED") {
|
||||
// delete the verificationId from the local storage
|
||||
await clearOtpCreds(mobile);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -224,7 +224,7 @@ export const authRouter = router({
|
|||
otp: z.string(),
|
||||
}))
|
||||
.mutation(async ({ input, ctx }): Promise<UserOtpVerifyResponse> => {
|
||||
const verificationId = getOtpCreds(input.mobile);
|
||||
const verificationId = await getOtpCreds(input.mobile);
|
||||
if (!verificationId) {
|
||||
throw new ApiError("OTP not sent or expired", 400);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,3 +105,7 @@ crons = [
|
|||
|
||||
[build]
|
||||
upload_source_maps = true
|
||||
|
||||
[[kv_namespaces]]
|
||||
binding="freshyo_otp_dev"
|
||||
id="6f6dbada52584d7fb744c9c85bd0c1e5"
|
||||
|
|
@ -98,3 +98,7 @@ crons = ["0 16 * * *", "0 1 * * *"]
|
|||
|
||||
[build]
|
||||
upload_source_maps = true
|
||||
|
||||
[[kv_namespaces]]
|
||||
binding="freshyo_otp"
|
||||
id="aba74be264df45c0b17757fe01c136e5"
|
||||
Loading…
Add table
Reference in a new issue