我有一个简单的聊天应用程序,其中的身份验证与 Whatsapp 完全一样
获取电话号码=>如果不存在则创建其他号码跳过=>发送验证码并将其设置为用户模型中的“phone_code”字段=>最后删除“phone_code”(如果经过验证)
该应用程序是在 React Native 中构建的,并以 Rest 框架作为 API,我对此很陌生,并且我正在努力在没有密码的情况下获取身份验证令牌。我使用 djangorestframework-simplejwt
我的寄存器视图:
@api_view(('POST',))
def register(request):
if request.method == 'POST':
serializer = UserSerializer(data=request.data)
if not serializer.is_valid():
if 'is not valid' in serializer.errors['phone_number'][0]:
return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
phone_number = serializer.initial_data['phone_number'].replace(' ', '')
try:
user = User.objects.get(phone_number=phone_number)
except User.DoesNotExist:
user = User.objects.create_user(
phone_number=phone_number, username=phone_number)
user.phone_code = randint(99999, 999999)
user.save()
TokenObtainPairView()
return Response(serializer.data, status.HTTP_200_OK)
# todo send validation code, I will handle later
我的登录视图(Chich 验证验证码)
@api_view(['POST',])
def loginuser(request):
if request.method == 'POST':
phone_number = request.data.get('phone_number')
try:
user = User.objects.get(phone_number=phone_number)
if int(request.data.get('phone_code')) == user.phone_code and user.phone_code:
user.phone_code = None
user.save()
#!!!!!!!!!!!!!!!!!!!NOW HOW CAN I GET THE JWT AUTHENTICATION TOKEN AND SEND IT TO MY REACT NATIVE APP?!
return JsonResponse({'phone_number': phone_number}, status=200)
当用户验证他的电话号码时,如何将 jwt 格式的身份验证令牌与响应一起发送?
urls.py:
path('api/token/', users_views.ObtainToken.as_view(), name='token_obtain_pair'),
我的自定义 acquireToken 视图:
class ObtainToken(TokenObtainPairView):
permission_classes = (AllowAny,)
serializer_class = MyTokenObtainPairSerializer
我还发现,当我使用 Postman 时,如果我发送空密码,系统将给出身份验证令牌。我将不胜感激任何帮助,谢谢
PIPIONE
相关分类