Spaces:
Build error
Build error
| import random | |
| from app.database import redis_client | |
| from fastapi import HTTPException | |
| async def generate_and_save_otp(mobile: str): | |
| otp = random.randint(100000, 999999) | |
| # Rate limiting (1 OTP per minute) | |
| if await redis_client.get(f"rate_limit:{mobile}"): | |
| raise HTTPException(status_code=429, detail="Too many requests. Try again later.") | |
| # Save OTP in Redis for 5 minutes | |
| await redis_client.setex(f"otp:{mobile}", 300, otp) | |
| await redis_client.setex(f"rate_limit:{mobile}", 60, 1) | |
| # Simulate sending OTP | |
| print(f"OTP for {mobile}: {otp}") | |
| return otp | |
| async def validate_otp(mobile: str, otp: str): | |
| stored_otp = await redis_client.get(f"otp:{mobile}") | |
| if not stored_otp or stored_otp != otp: | |
| raise HTTPException(status_code=400, detail="Invalid or expired OTP") | |
| await redis_client.delete(f"otp:{mobile}") |