1
我们正在为登录模块使用django rest框架和电话号码。如何使用Django Rest Framework中的移动号码登录。
要验证相同,我想使用OTP(一次性密码)。 这是如何实现的。
这可以使用rest框架的auth-token来完成。但是,这似乎并不是一个好主意。
任何其他方法。 请帮忙。
我们正在为登录模块使用django rest框架和电话号码。如何使用Django Rest Framework中的移动号码登录。
要验证相同,我想使用OTP(一次性密码)。 这是如何实现的。
这可以使用rest框架的auth-token来完成。但是,这似乎并不是一个好主意。
任何其他方法。 请帮忙。
对于移动和OTP与您的项目工作,你将需要与mobile
为username
和otp
为password
场创建own custom User
model。
在创建您自己的User
模型后,您可以使用移动设备和otp创建User
实例后,然后create a new Token。
您可以创建两个简单视图,您可以在用户输入有效手机号码之后首先生成otp,然后在响应中发送token
以验证otp。
An example on how to create a custom User model
例:
# views
@api_view(['POST'])
def generate_otp(request):
mobile = request.data.get('mobile', None)
if mobile:
otp = random.randrange(lower_limit, upper_limit+1)
User.objects.create(mobile=mobile, otp=otp)
return Response({'success': True})
else:
...
@api_view(['POST'])
def verify_otp(request):
mobile = ...
otp = request.data.get('otp', None)
if mobile and otp:
try:
user = User.objects.get(mobile=mobile, otp=otp)
except User.DoesNotExist:
return Response({'message': 'Invalid OTP'})
token = Token.objects.create(user=user)
return Response({'token': token})
else:
...
这是基于rest_framework authtoken
,你也可以用JWT,如果你想。
您使用基于令牌的身份验证吗? –
是的。我使用相同的。 – user2597209