Spaces:
Sleeping
Sleeping
Commit ·
9e0fc4a
1
Parent(s): 3542545
Refactor OTP handling to continue with email fallback on SMS failure; add timeout parameter to email OTP function and improve SMS client initialization
Browse files- app/services/otp_service.py +7 -8
- app/utils/email_utils.py +2 -2
- app/utils/sms_utils.py +3 -2
app/services/otp_service.py
CHANGED
|
@@ -27,14 +27,13 @@ class BookMyServiceOTPModel:
|
|
| 27 |
print(f"✅ OTP {otp} sent to {phone}. SID: {sid}")
|
| 28 |
except Exception as sms_error:
|
| 29 |
print(f"⚠️ SMS failed: {sms_error}")
|
| 30 |
-
if
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
raise HTTPException(status_code=500, detail="SMS failed and no fallback available.")
|
| 38 |
|
| 39 |
@staticmethod
|
| 40 |
async def verify_otp(identifier: str, otp: str):
|
|
|
|
| 27 |
print(f"✅ OTP {otp} sent to {phone}. SID: {sid}")
|
| 28 |
except Exception as sms_error:
|
| 29 |
print(f"⚠️ SMS failed: {sms_error}")
|
| 30 |
+
# Continue even if SMS fails
|
| 31 |
+
if is_email(identifier):
|
| 32 |
+
try:
|
| 33 |
+
send_email_otp(identifier, otp)
|
| 34 |
+
print(f"✅ OTP {otp} sent to {identifier} via email fallback.")
|
| 35 |
+
except Exception as email_error:
|
| 36 |
+
print(f"⚠️ Email failed: {email_error}")
|
|
|
|
| 37 |
|
| 38 |
@staticmethod
|
| 39 |
async def verify_otp(identifier: str, otp: str):
|
app/utils/email_utils.py
CHANGED
|
@@ -2,13 +2,13 @@ import smtplib
|
|
| 2 |
from email.mime.text import MIMEText
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
-
async def send_email_otp(to_email: str, otp: str):
|
| 6 |
msg = MIMEText(f"Your OTP is {otp}. It is valid for 5 minutes.")
|
| 7 |
msg["Subject"] = "Your One-Time Password"
|
| 8 |
msg["From"] = settings.SMTP_FROM
|
| 9 |
msg["To"] = to_email
|
| 10 |
|
| 11 |
-
server = smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT)
|
| 12 |
server.connect(settings.SMTP_HOST, settings.SMTP_PORT)
|
| 13 |
server.starttls()
|
| 14 |
server.login(settings.SMTP_USER, settings.SMTP_PASS)
|
|
|
|
| 2 |
from email.mime.text import MIMEText
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
+
async def send_email_otp(to_email: str, otp: str, timeout: float = 10.0):
|
| 6 |
msg = MIMEText(f"Your OTP is {otp}. It is valid for 5 minutes.")
|
| 7 |
msg["Subject"] = "Your One-Time Password"
|
| 8 |
msg["From"] = settings.SMTP_FROM
|
| 9 |
msg["To"] = to_email
|
| 10 |
|
| 11 |
+
server = smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT, timeout=timeout)
|
| 12 |
server.connect(settings.SMTP_HOST, settings.SMTP_PORT)
|
| 13 |
server.starttls()
|
| 14 |
server.login(settings.SMTP_USER, settings.SMTP_PASS)
|
app/utils/sms_utils.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
|
| 2 |
from twilio.rest import Client
|
|
|
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
def send_sms_otp(phone: str, otp: str) -> str:
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
message = client.messages.create(
|
| 9 |
from_=settings.TWILIO_SMS_FROM,
|
|
|
|
|
|
|
| 1 |
from twilio.rest import Client
|
| 2 |
+
from twilio.http.http_client import TwilioHttpClient
|
| 3 |
from app.core.config import settings
|
| 4 |
|
| 5 |
def send_sms_otp(phone: str, otp: str) -> str:
|
| 6 |
+
http_client = TwilioHttpClient(timeout=10) # 10 seconds timeout
|
| 7 |
+
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN, http_client=http_client)
|
| 8 |
|
| 9 |
message = client.messages.create(
|
| 10 |
from_=settings.TWILIO_SMS_FROM,
|