Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- url(r'^login/$', views.UserLoginAPIView.as_view(), name='login'),
- url(r'^api/auth/token/', obtain_jwt_token),
- class UserLoginSerializer(ModelSerializer):
- token = CharField(allow_blank=True, read_only= True)
- email = EmailField(label='Email Address', allow_blank= True)
- class Meta:
- model = User
- fields = [
- 'email',
- 'password',
- 'token'
- ]
- extra_kwargs = {"password":
- {"write_only": True}
- }
- def validate(self, data):
- user_obj = None
- email = data.get("email", None)
- password = data["password"]
- if not email:
- raise ValidationError('A username or email is required to login')
- user = User.objects.filter(
- Q(email=email)
- ).distinct()
- if user.exists() and user.count() == 1:
- user_obj = user.first()
- else:
- raise ValidationError("this email is not valid")
- if user_obj:
- if not user_obj.check_password(password):
- raise ValidationError("incorrect creadeintial try again")
- data["token"] = "SOME BLANK TOKEN"
- return data
- class UserLoginAPIView(APIView):
- permission_classes = [AllowAny]
- serializer_class = UserLoginSerializer
- def post(self, request, *args, **kwargs):
- data = request.data
- serializer = UserLoginSerializer(data=data)
- if serializer.is_valid(raise_exception=True):
- new_data = serializer.data
- return Response(new_data, status=HTTP_200_OK)
- return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement