Example #1
0
 def login(self, request):
     """
     默认的 username 是 admin, password 也是 admin
     """
     serializer = LoginSerializer(data=request.data)
     if not serializer.is_valid():
         return Response(
             {
                 "success": False,
                 "message": "Please check input",
                 "errors": serializer.errors,
             },
             status=400)
     username = serializer.validated_data['username']
     password = serializer.validated_data['password']
     user = django_authenticate(username=username, password=password)
     if not user or user.is_anonymous:
         return Response(
             {
                 "success": False,
                 "message": "username and password does not match",
             },
             status=400)
     django_login(request, user)
     return Response({
         "success": True,
         "user": UserSerializer(instance=user).data,
     })
Example #2
0
    def login(self, request):
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "massage": "Please check input.",
                    "errors": serializer.errors,
                },
                status=400)
        #validation ok , login
        username = serializer.validated_data['username']
        password = serializer.validated_data['password']

        if not User.objects.filter(username=username).exists():
            return Response(
                {
                    "success": False,
                    "massage": "User does not exists."
                },
                status=400)

        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    "success": False,
                    "massage": "Username and password does not match.",
                },
                status=400)
        django_login(request, user)
        return Response({
            "success": True,
            "user": UserSerializer(instance=user).data
        })
Example #3
0
    def login(self, request):
        # get username and password from request
        # data就是用户post的request中的数据
        # check request
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():  # 如果登录出错
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": serializer.errors,
                },
                status=400)

        # validation ok
        # check password
        username = serializer.validated_data['username']
        password = serializer.validated_data['password']
        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    "success": False,
                    "message": "Username and password does not match.",
                },
                status=400)

        # login
        django_login(request, user)
        return Response({
            "success": True,
            "user": UserSerializer(user).data,
        })
Example #4
0
    def login(self, request):
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(
                {
                    'success': False,
                    'message': "Please check input",
                    'errors': serializer.errors,
                },
                status=400)
        username = serializer.validated_data['username']
        password = serializer.validated_data['password']

        # queryset = User.objects.filter(username=username)
        # print(queryset.query)

        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    'success': False,
                    'message': "username and password does not match",
                },
                status=400)
        django_login(request, user)
        return Response({
            'success': True,
            'user': UserSerializer(instance=user).data
        })
Example #5
0
 def create(self, request):
     serializer = LoginSerializer(data=request.data)
     if not serializer.is_valid():
         return Response(
             {
                 'success': False,
                 'message': 'Please check input',
                 'errors': serializer.errors,
             },
             status=400)
     username = serializer.validated_data['username']
     password = serializer.validated_data['password']
     user = authenticate(username=username, password=password)
     if not user or user.is_anonymous:
         return Response(
             {
                 'success': False,
                 'message': 'username and password does not match',
             },
             status=400)
     login(request, user)
     return Response({
         'success': True,
         'user': UserSerializer(user).data,
     })
    def login(self, request):
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Input is not valid. Please check input",
                    "errors": serializer.errors,
                },
                status=400)

        username = serializer.validated_data['username']
        password = serializer.validated_data['password']

        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    "success":
                    False,
                    "message":
                    "Username and password are not matched. Please try again",
                },
                status=400)

        django_login(request, user)
        return Response({
            "success": True,
            "user": UserSerializer(user).data,
        })
Example #7
0
    def login(self, request):
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():
            return Response({
                "success": False,
                "error": serializer.errors,
            },
                            status=status.HTTP_400_BAD_REQUEST)

        # login
        username = serializer.validated_data['username']
        password = serializer.validated_data['password']
        user = django_authenticate(request,
                                   username=username,
                                   password=password)

        # username and password not match
        if not user or user.is_anonymous:
            return Response(
                {
                    "success": False,
                    "message": "Username and password not match"
                },
                status=status.HTTP_400_BAD_REQUEST)

        # valid username and password
        django_login(request, user)
        return Response(
            {
                "success": True,
                "user": UserSerializer(instance=user).data,
            },
            status=status.HTTP_200_OK)
Example #8
0
 def login(self, request):
     """
     默认的 username 是 admin, password 也是 admin
     """
     # get post request body
     serializer = LoginSerializer(data=request.data)
     if not serializer.is_valid():
         return Response({
             "success": False,
             "message": "Please check input.",
             "errors": serializer.errors,
         }, status=400)
     username = serializer.validated_data['username']
     password = serializer.validated_data['password']
     user = django_authenticate(username=username, password=password)
     # User.objects.filter(username=username) will be translated to a query
     # you can debug by
     # queryset = User.objects.filter(username=username)
     # print(queryset.query)
     if not User.objects.filter(username=username).exists():
         return Response({
             "success": False,
             "message": "Please check input."
         }, status=400)
     if not user or user.is_anonymous:
         return Response({
             "success": False,
             "message": "Username and password does not match.",
         }, status=400)
     django_login(request, user)
     return Response({
         "success": True,
         "user": UserSerializer(instance=user).data,
     })
Example #9
0
    def post(self, request, format='json'):
        serializer = LoginSerializer(data=request.data)

        if serializer.is_valid():
            username = request.data.get("username")
            password = request.data.get("password")
            user = authenticate(request, username=username, password=password)
            if user is not None:
                login(request, user)
                return Response(
                    {"token": Token.objects.get_or_create(user=user)[0].key},
                    status=status.HTTP_200_OK)
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Example #10
0
def login(request):
    serializer = LoginSerializer(data=request.data)
    if not serializer.is_valid():
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    username = serializer.validated_data['username']
    password = serializer.validated_data['password']

    user = authenticate(username=username, password=password)
    if not user:
        return Response({'details': 'BAD CREDENTIALS'},
                        status=status.HTTP_401_UNAUTHORIZED)

    django_login(request, user)
    return Response({'details': serializer.data},
                    status=status.HTTP_202_ACCEPTED)
Example #11
0
    def login(self, request):
        serializer = LoginSerializer(data=request.data)
        # handles invalid input
        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": serializer.errors
                },
                status=400)

        # if username does not exist
        username = serializer.validated_data['username'].lower()
        password = serializer.validated_data['password']

        if not User.objects.filter(username=username).exists():
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": {
                        "username": ["User does not exist."]
                    }
                },
                status=400)

        user = django_authenticate(request,
                                   username=username,
                                   password=password)
        # wrong password
        if not user:
            return Response(
                {
                    "success": False,
                    "message": "Username and password does not match."
                },
                status=400)

        # log user in
        django_login(request, user)
        return Response({
            "success": True,
            "user": UserSerializer(instance=user).data,
        })
Example #12
0
    def login(self, request):
        serializer = LoginSerializer(data=request.data)

        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": serializer.errors,
                },
                status=400)

        username = serializer.validated_data['username'].lower()
        password = serializer.validated_data['password']

        # check user exists
        if not User.objects.filter(username=username):
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": {
                        "username": ["User does not exist."]
                    }
                },
                status=400)

        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    "success": False,
                    "message": "Username and password does not match.",
                },
                status=400)

        django_login(request, user)

        user_data = {
            "id": user.id,
            "username": user.username,
            "email": user.email,
        }
        return Response({"success": True, "user": user_data})
Example #13
0
    def login(self, request):
        # User serializer to validate the input
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(
                {
                    'success': False,
                    'message': 'Please check input',
                    'errors': serializer.errors
                },
                status=400)

        # Authenticate via django authenticate
        username = serializer.validated_data['username']
        password = serializer.validated_data['password']

        if not User.objects.filter(username=username).exists():
            return Response(
                {
                    'success': False,
                    'message': 'Please check input.',
                    'errors': {
                        'username': ['User does not exist.']
                    }
                },
                status=400)

        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    'success': False,
                    'message': 'username and password do not match',
                },
                status=400)

        # login via django login
        django_login(request, user)
        return Response({
            'success': True,
            'user': UserSerializer(instance=user).data
        })
    def login(self, request):
        #get username and password from requests
        serializer = LoginSerializer(
            data=request.data)  # 如果是GET:request.query_params
        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Please Check input",
                    "errors": serializer.errors,
                },
                status=400)  #默认status是200
        #validation ok, login
        username = serializer.validated_data['username'].lower()
        password = serializer.validated_data['password']

        #user doesn't exist
        if not User.objects.filter(username=username).exists():
            # queryset=User.objects.filter(username=username)
            #print(queryset.query) 打印语句
            return Response(
                {
                    "success": False,
                    "message": "User does not exist.",
                },
                status=400)

        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    "success": False,
                    "message": "Username and password doesn not match.",
                },
                status=400)
        django_login(request, user)
        return Response(
            {
                "success": True,
                "user": UserSerializer(instance=user).data,
            },
            status=200)
Example #15
0
    def login(self, request):
        serializer = LoginSerializer(data=request.data)

        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Please check your input",
                    "errors": serializer.errors,
                },
                status=400)

        #validation of the user authentication
        username = serializer.validated_data['username']
        password = serializer.validated_data['password']

        # check exist of the user ---- optional [Security]
        if not User.objects.filter(username=username).exists():
            return Response(
                {
                    "success": False,
                    "message": "User doesn't exist",
                },
                status=400)

        # validate the password and username
        user = django_authenticate(username=username, password=password)
        if not user or user.is_anonymous:
            return Response(
                {
                    "success": False,
                    "message": "username and password doesn't match",
                },
                status=400)

        # login
        django_login(request, user)
        return Response({
            "success": True,
            "user": UserSerializer(instance=user).data,
        })
Example #16
0
 def login(self, request):
     # get username and password from request
     serializer = LoginSerializer(data=request.data)
     if not serializer.is_valid():
         return Response(
             {
                 "success": False,
                 "message": "Please check input",
                 "errors": serializer.errors,
             },
             status=400)
     # 400是bad request
     username = serializer.validated_data['username']
     password = serializer.validated_data['password']
     # if user doesn't exist
     if not User.objects.filter(username=username).exists():
         return Response(
             {
                 "success": False,
                 "message": "User does not exist.",
             },
             status=400)
     # if login info doesn't match
     user = django_authenticate(username=username, password=password)
     if not user or user.is_anonymous:
         return Response(
             {
                 "success": False,
                 "message": "Username or password does not match",
             },
             status=400)
     django_login(request, user)
     return Response({
         "success": True,
         "user": UserSerializer(instance=user).data,
     })