def login(request): if not request.data: return Response({'detail': 'Email and password required'}, status=status.HTTP_400_BAD_REQUEST) data = request.data login_validate = LoginSerializer(data=data) login_validate.is_valid(raise_exception=True) try: user = User.objects.get(email=data['email']) except User.DoesNotExist: raise exceptions.AuthenticationFailed("Wrong login credentials") if not user.check_password(data['password']): raise exceptions.AuthenticationFailed('Wrong login credentials') token, _ = Token.objects.get_or_create(user=user) if token_expire_handler(token): token.delete() token = Token.objects.create(user=token.user) serializer = UserSerializer(user) data = serializer.data data['token'] = token.key return Response({'data': data}, status=status.HTTP_200_OK)
def login(self, request): """Handles Login""" login_serializer = LoginSerializer(data=request.data) if not login_serializer.is_valid(): return Response({'details': login_serializer.errors}, status=status.HTTP_400_BAD_REQUEST) user = authenticate(username=login_serializer.data['username'], password=login_serializer.data['password']) if not user: return Response({ 'details': 'Invalid Credentials', 'code': 400 }, status=status.HTTP_200_OK) try: token = TokenModel.objects.get(user=user) except TokenModel.DoesNotExist: token = self._token_generator(user) is_expired, token = expired_token_handler(token) if is_expired: token = self._token_generator(user) user_serialized = CustomUserSerializer(user) return Response( { 'usertype': user_serialized.data['user_type'], 'expires_in': expires_in(token), 'token': token.key }, status=status.HTTP_200_OK)
def login(data): """ Function that logins an user in the API. :param data: JSON with the credentials: username and password. :return: JSON with the username and the token associated. """ login_serializer = LoginSerializer(data=data) if not login_serializer.is_valid(): response = ErrorResponseSerializer({'detail': 'Invalid body'}).data return False, response, None user = authenticate(username=login_serializer.data["username"], password=login_serializer.data["password"]) if not user: response = ErrorResponseSerializer({ 'detail': 'Invalid credentials' }).data return False, response, None token, _ = Token.objects.get_or_create(user=user) data = UserSerializer(user).data response = LoginResponseSerializer({'data': data, 'token': token}).data return True, None, response
def login(request): try: if request.method == 'POST': data = JSONParser().parse(request) serializer = LoginSerializer(data=data) if (serializer.is_valid()): logged_user = app.models.user.objects.get( username=data['username'], password=data['password']) serializer = UserSerializer(logged_user) return HttpResponse(JSONRenderer().render(serializer.data), status=202) except Exception as e: return HttpResponse(e, status=400)
def post(self, request, *args, **kwargs): serializer = LoginSerializer(data=request.data) serializer.is_valid(raise_exception=True) try: user = User.objects.get(email=request.data["email"]) auth_user = authenticate(username=user.username, password=request.data["password"]) token, created = Token.objects.get_or_create(user=auth_user) return Response( { "token": token.key, "username": auth_user.username, "email": auth_user.email, }, status=status.HTTP_200_OK, ) except User.DoesNotExist: return Response("this user does not exist", status=400)
def post(self, request): serializer = LoginSerializer(data=request.data) if serializer.is_valid(): try: user = User.objects.get(**serializer.validated_data) except: return Response({ 'status': 'error', 'message': 'unable to login with provided credential' }) token, c = Token.objects.get_or_create(user=user) return Response({ 'status': 'success', 'message': 'successfully loggedin', 'token': token.key, 'customer_id': user.id }) return Response({'status': 'errors', 'message': serializer.errors})