From 54b265f459e8ed256983950fb509a5f3f0e145fa Mon Sep 17 00:00:00 2001 From: shafi54 <108669266+shafi-aviz@users.noreply.github.com> Date: Mon, 10 Aug 2026 20:20:42 +0530 Subject: [PATCH] enh --- apps/backend/src/lib/env-exporter.ts | 7 ++++ apps/backend/src/lib/otp-utils.ts | 40 ++++++++++++------- .../src/trpc/apis/user-apis/apis/auth.ts | 2 +- apps/backend/wrangler.dev.toml | 4 ++ apps/backend/wrangler.prod.toml | 4 ++ 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/apps/backend/src/lib/env-exporter.ts b/apps/backend/src/lib/env-exporter.ts index 5a09676..2c85aa5 100755 --- a/apps/backend/src/lib/env-exporter.ts +++ b/apps/backend/src/lib/env-exporter.ts @@ -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) diff --git a/apps/backend/src/lib/otp-utils.ts b/apps/backend/src/lib/otp-utils.ts index 1d19148..d7b82be 100644 --- a/apps/backend/src/lib/otp-utils.ts +++ b/apps/backend/src/lib/otp-utils.ts @@ -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(); +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 { - 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; diff --git a/apps/backend/src/trpc/apis/user-apis/apis/auth.ts b/apps/backend/src/trpc/apis/user-apis/apis/auth.ts index 97b81db..195dfcf 100644 --- a/apps/backend/src/trpc/apis/user-apis/apis/auth.ts +++ b/apps/backend/src/trpc/apis/user-apis/apis/auth.ts @@ -225,7 +225,7 @@ export const authRouter = router({ otp: z.string(), })) .mutation(async ({ input, ctx }): Promise => { - const verificationId = getOtpCreds(input.mobile); + const verificationId = await getOtpCreds(input.mobile); if (!verificationId) { throw new ApiError("OTP not sent or expired", 400); } diff --git a/apps/backend/wrangler.dev.toml b/apps/backend/wrangler.dev.toml index b05b479..c2e0064 100644 --- a/apps/backend/wrangler.dev.toml +++ b/apps/backend/wrangler.dev.toml @@ -103,3 +103,7 @@ crons = [ [build] upload_source_maps = true + +[[kv_namespaces]] +binding="freshyo_otp_dev" +id="6f6dbada52584d7fb744c9c85bd0c1e5" \ No newline at end of file diff --git a/apps/backend/wrangler.prod.toml b/apps/backend/wrangler.prod.toml index 14446a5..6eec64a 100644 --- a/apps/backend/wrangler.prod.toml +++ b/apps/backend/wrangler.prod.toml @@ -98,3 +98,7 @@ crons = ["0 16 * * *", "0 1 * * *"] [build] upload_source_maps = true + +[[kv_namespaces]] +binding="freshyo_otp" +id="aba74be264df45c0b17757fe01c136e5" \ No newline at end of file