| 1234567891011121314151617181920212223242526272829303132333435363738 |
- import random
- import string
- from app.core.cache import redis_client
- class SmsService:
- @staticmethod
- def send_code(mobile: str) -> str:
- """
- Generates and 'sends' a 6-digit code.
- Returns the code for dev/mock purposes (or logs it).
- """
- code = ''.join(random.choices(string.digits, k=6))
-
- # Store in Redis: SMS:{mobile} -> code
- key = f"SMS:{mobile}"
- # Expire in 5 minutes
- redis_client.setex(key, 300, code)
-
- # In production, call actual SMS provider here.
- print(f"=======================================")
- print(f" [MOCK SMS] To: {mobile}, Code: {code}")
- print(f"=======================================")
-
- return code
- @staticmethod
- def verify_code(mobile: str, code: str) -> bool:
- key = f"SMS:{mobile}"
- stored_code = redis_client.get(key)
-
- if not stored_code:
- return False
-
- if stored_code == code:
- redis_client.delete(key) # One-time use
- return True
- return False
|