Ejemplo n.º 1
0
    def login(self, request, *args, **kwargs):
        """Авторизация.

        Полученный токен добавить в заголовок:
        HTTP_AUTHORIZATION: Token <TOKEN>

        Raises:
            AuthenticationFailed: email or password invalid
        """
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = authenticate(email=serializer.data['email'],
                            password=serializer.data['password'])

        if not user:
            raise AuthenticationFailed()

        user_token, created = UserToken.objects.get_or_create(user=user)
        if not created:
            user_token.refresh_token()

        serializer = TokenSerializer(
            data={
                'token': user_token.key,
                'user_id': user_token.user.pk,
                'first_name': user.first_name,
                'last_name': user.last_name,
            })
        serializer.is_valid(raise_exception=True)
        return Response(serializer.data)
Ejemplo n.º 2
0
 def post(self, request):
     serializer = TokenSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data['user']
     try:
         token, created = Token.objects.get_or_create(user=user)
         data = {'token': str(token)}
         return Response(data)
     except:
         return Response({
             "non_field_errors": ["Username or password are not correct."]
         })
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()

        if user is not None:
            serializer = TokenSerializer(data={
                # using drf jwt utility functions to generate a token
                "token": jwt_encode_handler(
                    jwt_payload_handler(user)
                )})
            serializer.is_valid()
            return Response(serializer.validated_data)
        return Response(status=status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 4
0
 def get(self, request, *args, **kwargs):
     print(request)
     serializer = self.serializer_class(data=request.data,
                                        context={"request": request})
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data["user"]
     data = TokenSerializer(Token.objects.get(user=user)).data
     return JsonResponse(data=data, code="0", msg="oka")
Ejemplo n.º 5
0
 def post(self, request, *args, **kwargs):
     data = request.data
     serializer = UserLoginSerializer(data=data)
     if serializer.is_valid(raise_exception=True):
         user = User.objects.filter(
             username=serializer.data['username']).first()
         token, _ = Token.objects.get_or_create(user=user)
         return Response(data=TokenSerializer(token).data,
                         status=status.HTTP_200_OK)
     return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
Ejemplo n.º 6
0
 def post(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     if serializer.is_valid():
         user = serializer.user
         token, _ = Token.objects.get_or_create(user=user)
         return Response(data=TokenSerializer(token).data,
                         status=status.HTTP_200_OK)
     else:
         return Response(
             data=serializer.errors,
             status=status.HTTP_400_BAD_REQUEST,
         )
Ejemplo n.º 7
0
    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            user = serializer.user
            token, _ = Token.objects.get_or_create(user=user)
            login(request, user)
            data = {}
            data['auth_token'] = TokenSerializer(token).data["auth_token"]
            data['user'] = request.user.id
            return Response(data, status=status.HTTP_200_OK)

        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 8
0
 def post(self, request, *args, **kwargs):
     print request.data
     serializer = self.get_serializer(data=request.data)
     if serializer.is_valid():
         user = serializer.user
         token, _ = Token.objects.get_or_create(user=user)
         return Response(
             data={
                 "username": user.username,
                 "token": TokenSerializer(token).data["auth_token"]
             },
             status=status.HTTP_200_OK,
         )
     else:
         return Response(
             data=serializer.errors,
             status=status.HTTP_400_BAD_REQUEST,
         )
Ejemplo n.º 9
0
    def post(self, request, *args, **kwargs):
        if not request.data.get('password') or not request.data.get(
                'username'):
            raise ParseError({
                "error_code":
                "400_PASSWORD_USER",
                "message":
                "username va password khong duoc de trong"
            })

        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            user = serializer.user
            token, _ = Token.objects.get_or_create(user=user)

            data = TokenSerializer(token).data

            # data_user = {'username' : user.username,'email' : user.email}
            data_user = {
                'id': user.id,
                'username': user.username,
            }
            data_user.update(data)

            data_all = {
                "error_code": 0,
                "messages": "login success",
                "data": data_user
            }
            response = Response(
                data=data_all,
                status=status.HTTP_200_OK,
            )
            tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
            tomorrow = datetime.datetime.replace(tomorrow,
                                                 hour=0,
                                                 minute=0,
                                                 second=0)
            expires = datetime.datetime.strftime(tomorrow,
                                                 "%a, %d-%b-%Y %H:%M:%S GMT")
            response.set_cookie('token', token, expires=expires, httponly=True)
            return response
Ejemplo n.º 10
0
 def post(self, request):
     serializer = TokenSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data, status=status.HTTP_200_OK)
Ejemplo n.º 11
0
def profile_api(request, *args, **kwargs):
    serializer = TokenSerializer(Token.objects.get(user=request.user.id))
    return Response(serializer.data)