fix login and jwt

This commit is contained in:
javad committed 2026-10-09 10:07:31 +03:30
1 parent 56d8760613
commit 48d0ecba79
4 files changed
+104 -3

No files matched your search

+16
View File
@@ -13,6 +13,14 @@ from utils.services.sms import DefaultSmsProvider
from utils.validations.mobile import normalize_mobile
CUser = get_user_model()
SMS_RESEND_INTERVAL = 60
class SmsRequestThrottled(exceptions.Throttled):
def __init__(self, wait):
super().__init__(wait=wait)
self.detail = {'detail': 'کد ورود قبلاً ارسال شده است. کمی صبر کنید و کد قبلی را وارد کنید.',
'retry_after': wait}
class SmsDeliveryUnavailable(exceptions.APIException):
@@ -77,6 +85,14 @@ def login_by_mobile(mobile: int):
# Serialize code requests for this account, retaining the old valid code if
# the provider refuses the new message.
CUser.objects.select_for_update().get(pk=user.pk)
# Recheck eligibility after waiting for concurrent requests/administrative edits.
user, _ = mobile_user(user.mobile)
current = SmsOpt.objects.filter(mobile=user.mobile, expire__gt=timezone.now()).order_by('-created_at').first()
if current:
remaining = SMS_RESEND_INTERVAL - (timezone.now() - current.created_at).total_seconds()
if remaining > 0:
from math import ceil
raise SmsRequestThrottled(ceil(remaining))
code = secrets.randbelow(900000) + 100000
if not DefaultSmsProvider().send_sms(str(user.mobile), f'کد ورود شما: {code}'):
raise SmsDeliveryUnavailable()