SHARE
TWEET

Untitled

a guest Mar 14th, 2019 199 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @csrf_exempt
  2. def login_user(request):
  3.     if request.method == "POST":
  4.         data = JSONParser().parse(request)
  5.         email = data.get('email')
  6.         password = data.get('password').encode('utf-8')
  7.         try:
  8.             user = User.objects.get(email=email)
  9.         except User.DoesNotExist:
  10.             return JsonResponse({"errors": "Invalid Credentials"})
  11.        
  12.         serializer = UserSerializer(user)
  13.         user_data = serializer.data
  14.         hashed_password = user_data.get('password').encode('utf-8')
  15.         if bcrypt.checkpw(password, hashed_password):
  16.             token = create_jwt(email)
  17.             return JsonResponse({"message": "User Login Successful", "token": token}, status=200)
  18.         else:
  19.             return JsonResponse({"errors": "Invalid Credentials"})
  20.     else:
  21.     return JsonResponse({"errors": "You must send a POST request"})
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top