Esempio n. 1
0
 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)
Esempio n. 2
0
 def login(self, request):
     """Login action."""
     serializer = UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user, token = serializer.save()
     data = {'token': token, 'user': self.get_serializer(user).data}
     return Response(data, status=status.HTTP_200_OK)
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        """Handle HTTP POST request."""

        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)
Esempio n. 4
0
 def create(self, request):
     serializer = UserLoginSerializer(data=request.POST)
     if serializer.is_valid(raise_exception=True):
         user = authenticate(**serializer.validated_data)
         if user and user.has_activated_email:
             token, created = Token.objects.get_or_create(user=user)
             return Response(
                 ResponseStatuses.SUCCESSFULLY_LOGINED.update({
                     'token':
                     token.key,
                 }))
         if not cache.get(f'{user.username}_activation_link_was_resent',
                          False):
             webhook_url = reverse('email-confirm', request=request)
             resend_activation_url.apply_async(args=(user.username,
                                                     user.email,
                                                     webhook_url))
             return Response(ResponseStatuses.EMAIL_NOT_ACTIVATED,
                             status=status.HTTP_401_UNAUTHORIZED)
     return Response(status=status.HTTP_401_UNAUTHORIZED)
Esempio n. 5
0
    def render(self, data, accepted_media_type=None, renderer_context=None):
        response_data = dict()

        if data.get('token', None):
            response_data['data'] = {'token': data['token']}
            valid_data = VerifyJSONWebTokenSerializer().validate(
                response_data['data'])
            user_to_dict = UserLoginSerializer(
                valid_data['user'],
            ).instance.__dict__
            del user_to_dict['_state'], \
                user_to_dict['password'], \
                user_to_dict['is_superuser']
            response_data['data']['user'] = user_to_dict

        elif data.get('is_reserved', None) is not None:
            response_data['data'] = data
        else:
            response_data = get_error_response(data)
        renderer_context = renderer_context or {}
        response = super().render(
            response_data, accepted_media_type, renderer_context)
        return response