I am using just phone number alone as the only field for login. So I am trying to write my custom backend authentication. On trying to import the LoginBackend module, I am getting the error below.
ERROR WHEN I CONFIGURE THE AUTH BACKENDS:
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 161, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
@csrf_exempt
@api_view(["POST"])
@permission_classes((AllowAny,))
def logins(request):
phone_number = request.data.get("phone_number")
if phone_number is None:
return Response({'error': 'Please provide your phone number'},
status=HTTP_400_BAD_REQUEST)
user = authenticate(phone_number=phone_number)
if not user:
return Response({'error': 'Invalid Credentials'},
status=HTTP_404_NOT_FOUND)
token, _ = Token.objects.get_or_create(user=user)
return Response({'token': token.key},
status=HTTP_200_OK)
backends.py
from django.contrib.auth.backends import ModelBackend
from .models import User
class LoginBackend(ModelBackend):
def authenticate(self, request, **kwargs):
phone_number= kwargs['phone_number']
user = User.objects.get(phone_number=phone_number)
if user:
return user
else:
return None
settings.py
from .backends import LoginBackend
from django.contrib.auth.backends import ModelBackend
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend',
'findr.backends.LoginBackend']
使这些东西正常工作所需要做的就是撤消 settings.py 中的导入语句。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.