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)
def login(self, request): """Login allow user to gain access into the system """ serializer = UserLoginSerializer(data=request.data) serializer.is_valid(raise_exception=True) user, token = serializer.save() data = {'user': UserModelSerializer(user).data, 'access_token': token} return Response(data=data, status=status.HTTP_200_OK)
def login(self, request): """User sign in.""" serializer = UserLoginSerializer(data=request.data) serializer.is_valid(raise_exception=True) user, token = serializer.save() data = { 'user': UserModelSerializer(user).data, 'access_token': token, } return Response(data, status=status.HTTP_201_CREATED)
def post(self, request): serializer = UserLoginSerializer(data=request.data) if serializer.is_valid(): data = serializer.validated_data responseJson = { "id": data["id"], "username": data["username"], "email": data["email"], "token": data["token"] } return Response(responseJson, status=HTTP_200_OK) return Response(status=HTTP_400_BAD_REQUEST)
def get_user_info(request): """ This API returns a user object once authentication against a username and password. """ serialized_data = UserLoginSerializer(data=request.data) status_code = 401 data = dict() if serialized_data.is_valid(): user_login = UserLogin() user_login.username = serialized_data.data['username'] user_login.password = serialized_data.data['password'] user = authenticate(username=user_login.username, password=user_login.password) if user: serialized = UserSerializer(user) data = serialized.data status_code = 200 return JsonResponse(data, status=status_code)\
def login(self, request): ''' ENDPOINT : /api/users/login/ ''' serializer = UserLoginSerializer(data=request.data) if serializer.is_valid(): user = authenticate(username=request.data.get('email'), password=request.data.get('password')) if user and user.is_active: token = self.refresh_token(user) r_text = token.token r_status = status.HTTP_200_OK else: r_text = 'Invalid Username/Password' r_status = status.HTTP_401_UNAUTHORIZED else: r_text = serializer.errors r_status = status.HTTP_400_BAD_REQUEST return Response(r_text, status=r_status)
def post(self, request): serializer = UserLoginSerializer(data=request.data) if serializer.is_valid(): username = serializer.validated_data['username'] password = serializer.validated_data['password'] user = authenticate(username=username, password=password) if user is None: raise serializers.ValidationError( "Invalid username/password. Please try again!") else: login(request, user) token, created = Token.objects.get_or_create(user=user) return Response({ 'token': token.key, 'user_id': user.pk, 'email': user.email }, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): response=super(CustomObtainAuthToken,self).post(request,*args,**kwargs) token=Token.objects.get(key=response.data['token']) user=User.objects.get(id=token.user_id) serializer=UserLoginSerializer(user,many=True) return Response({ 'token': token.key, 'user_id': user.pk, 'username':user.username, 'email': user.email })
def my_jwt_response_handler(token, user=None, request=None): current_user = CustomUser.objects.get( user__username=UserLoginSerializer(user, context={ 'request': request }).data['username']) returnData = { 'token': token, 'user': { 'pk': False, 'username': False, #'credential':False, } } if current_user: returnData['user']['pk'] = current_user.pk returnData['user']['username'] = current_user.user.username #returnData['user']['credential'] = current_user.credential return returnData
def post(self, request): try: if 'token' in request.data: serializer = UserLoginSerializerToken(data=request.data) else: serializer = UserLoginSerializer(data=request.data) if serializer.is_valid(): data = serializer.validated_data username = data["username"] password = data["password"] user = authenticate(username=username, password=password) if user: login(request, user) del data["password"] data["status"] = 'success' else: data = {} data["status"] = 'fail' data["error"] = serializer.errors return Response(data) except Exception as e: data = {'invalid': 'Invalid or expired token'} return Response(data, status=status.HTTP_400_BAD_REQUEST)
def test_serialized_data(self): user = UserLoginSerializer(self.data).data self.assertTrue(user == self.data, msg='User not serialized')