feat: 实现邮箱验证码登录功能
This commit is contained in:
parent
57f300e1a7
commit
f142718cd5
|
|
@ -12,6 +12,26 @@ def check_phone_code(phone, code, raise_exception=True):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def check_email_code(email, code, raise_exception=True):
|
||||||
|
"""验证邮箱验证码
|
||||||
|
|
||||||
|
Args:
|
||||||
|
email: 邮箱地址
|
||||||
|
code: 验证码
|
||||||
|
raise_exception: 是否抛出异常
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 验证码是否正确
|
||||||
|
"""
|
||||||
|
cache_key = f'email_code_{email}'
|
||||||
|
code_exist = cache.get(cache_key, None)
|
||||||
|
if code_exist == code:
|
||||||
|
return True
|
||||||
|
if raise_exception:
|
||||||
|
raise ParseError('验证码错误或已过期')
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def validate_password(password):
|
def validate_password(password):
|
||||||
# 正则表达式匹配规则
|
# 正则表达式匹配规则
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ from django.urls import path
|
||||||
from rest_framework_simplejwt.views import TokenRefreshView
|
from rest_framework_simplejwt.views import TokenRefreshView
|
||||||
|
|
||||||
from apps.auth1.views import (CodeLogin, LoginView, LogoutView, PwResetView,
|
from apps.auth1.views import (CodeLogin, LoginView, LogoutView, PwResetView,
|
||||||
SecretLogin, SendCode, TokenBlackView, WxLogin, WxmpLogin, TokenLoginView, FaceLoginView)
|
SecretLogin, SendCode, TokenBlackView, WxLogin, WxmpLogin, TokenLoginView, FaceLoginView, EmailCodeLogin)
|
||||||
from apps.auth1.register_view import RegisterView
|
from apps.auth1.register_view import RegisterView
|
||||||
from apps.auth1.email_code_view import EmailCodeView
|
from apps.auth1.email_code_view import EmailCodeView
|
||||||
|
|
||||||
|
|
@ -22,5 +22,6 @@ urlpatterns = [
|
||||||
path(API_BASE_URL + 'reset_password/', PwResetView.as_view(), name='reset_password'),
|
path(API_BASE_URL + 'reset_password/', PwResetView.as_view(), name='reset_password'),
|
||||||
path(API_BASE_URL + 'login_face/', FaceLoginView.as_view(), name='face_login'),
|
path(API_BASE_URL + 'login_face/', FaceLoginView.as_view(), name='face_login'),
|
||||||
path(API_BASE_URL + 'email_code/', EmailCodeView.as_view(), name='email_code'),
|
path(API_BASE_URL + 'email_code/', EmailCodeView.as_view(), name='email_code'),
|
||||||
|
path(API_BASE_URL + 'login_email_code/', EmailCodeLogin.as_view(), name='login_email_code'),
|
||||||
path(API_BASE_URL + 'register/', RegisterView.as_view(), name='register')
|
path(API_BASE_URL + 'register/', RegisterView.as_view(), name='register')
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ from rest_framework.permissions import IsAuthenticated
|
||||||
from apps.auth1.errors import USERNAME_OR_PASSWORD_WRONG
|
from apps.auth1.errors import USERNAME_OR_PASSWORD_WRONG
|
||||||
from rest_framework_simplejwt.tokens import RefreshToken
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from apps.auth1.services import check_phone_code
|
from apps.auth1.services import check_phone_code, check_email_code
|
||||||
|
|
||||||
from apps.utils.tools import rannum
|
from apps.utils.tools import rannum
|
||||||
from apps.utils.wxmp import wxmpClient
|
from apps.utils.wxmp import wxmpClient
|
||||||
|
|
@ -212,6 +212,26 @@ class CodeLogin(CreateAPIView):
|
||||||
raise ParseError('账户不存在或已禁用')
|
raise ParseError('账户不存在或已禁用')
|
||||||
|
|
||||||
|
|
||||||
|
class EmailCodeLogin(CreateAPIView):
|
||||||
|
"""邮箱验证码登录
|
||||||
|
|
||||||
|
邮箱验证码登录
|
||||||
|
"""
|
||||||
|
authentication_classes = []
|
||||||
|
permission_classes = []
|
||||||
|
serializer_class = EmailCodeLoginSerializer
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
email = request.data['email']
|
||||||
|
code = request.data['code']
|
||||||
|
check_email_code(email, code)
|
||||||
|
user = User.objects.filter(username=email).first()
|
||||||
|
if user:
|
||||||
|
ret = get_tokens_for_user(user)
|
||||||
|
return Response(ret)
|
||||||
|
raise ParseError('账户不存在或已禁用')
|
||||||
|
|
||||||
|
|
||||||
class SecretLogin(CreateAPIView):
|
class SecretLogin(CreateAPIView):
|
||||||
"""App端密钥登录
|
"""App端密钥登录
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue