예제 #1
0
    def post(self, request):
        req_data = request.data
        login_serializer = UserLoginSerializer(data=req_data)
        login_serializer.is_valid(raise_exception=True)
        # if no validation error
        # logging in the user
        user = authenticate(email=req_data.get('email'),
                            password=req_data.get('password'))
        login(request, user)

        return Response(data=login_serializer.data, status=status.HTTP_200_OK)
예제 #2
0
 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)
예제 #3
0
 def post(self, request, *args, **kwargs):
     data = request.data
     serializer = UserLoginSerializer(data=data)
     if serializer.is_valid(raise_exception=True):
         username = serializer.data['username']
         user = User.objects.filter(username=username).first()
         token = Token.objects.filter(user=user).first().key
         return Response({'token': token}, status=HTTP_200_OK)
     return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
예제 #4
0
    def post(self, request, format='json'):

        data = request.data
        serializer = UserLoginSerializer(data=data)
        if serializer.is_valid():
            user = serializer.validated_data['user']
            login(request, user)
            token, create = Token.objects.get_or_create(user=user)
            print(token)
            return Response({"token":token.key}, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #5
0
    def post(self, request, format=None, *args, **kwargs):
        data = request.data
        serializer = UserLoginSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            kwargs = {
                'username': new_data['username'],
                'password': new_data['password'],
            }
            user = auth.authenticate(**kwargs)
            extra_data = {}
            auth.login(request, user)
            token, _ = Token.objects.get_or_create(user=user)
            extra_data['token'] = token.key

            return Response(extra_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
예제 #6
0
    def post(self, request, *args, **kwargs):
        serialized_data = UserLoginSerializer(data=request.data)
        if serialized_data.is_valid():

            user = auth.authenticate(request, **serialized_data.data)

            if user is not None:
                auth.login(request, user)
                user_serialized = UserSerializer(user)

                return Response(data=user_serialized.data,
                                status=status.HTTP_200_OK)

            return Response(data={'message': 'invalid credentials'},
                            status=status.HTTP_404_NOT_FOUND)

        return Response(
            data={'message': 'initial credential validation failed'},
            status=status.HTTP_400_BAD_REQUEST)
예제 #7
0
    def post(self, request):
        response_data = {}
        serializer = UserLoginSerializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            user = serializer.validated_data['user']
            token, created = Token.objects.get_or_create(user=user)
            user_logged_in.send(sender=user.__class__,
                                request=request,
                                user=user)
            update_last_login(None, user)
            response_data['details'] = 'successfully logged in'
            # response_data['email'] = user.email
            # response_data['fullname'] = user.fullname
            # response_data['username'] = user.username
            # response_data['avatar'] = request.build_absolute_uri(user.avatar.url)
            #response_data['pk'] = user.pk
            response_data['token'] = token.key
        else:
            response_data = serializer.errors

        return Response(response_data)
예제 #8
0
 def post(self,request):
     serializer=UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data['user']
     token, created = Token.objects.get_or_create(user=user)
     return Response({'token': token.key},status=status.HTTP_200_OK)
예제 #9
0
 def post(self, request, *args, **kwargs):
     serializer = UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     token = serializer.save()
     data = {'status': 'ok', 'token': token}
     return Response(data, status=status.HTTP_201_CREATED)
예제 #10
0
    def post(self, request):
        req_data = request.data
        login_serializer = UserLoginSerializer(data=req_data)
        login_serializer.is_valid(raise_exception=True)

        return Response(data=login_serializer.data, status=status.HTTP_200_OK)