[Fixed]-Django drf simple-jwt authentication"detail": "No active account found with the given credentials"

29πŸ‘

Ensure your password is being hashed before it is stored in your db. I ran into the same problem and discovered my passwords were being stored in plain text. Adding the following to my UserSerializer solved the issue

from django.contrib.auth.hashers import make_password

def validate_password(self, value: str) -> str:
    """
    Hash value passed by user.

    :param value: password of a user
    :return: a hashed version of the password
    """
    return make_password(value)

4πŸ‘

Either you did not create a superuser for your Django application or you are provided the wrong credentials for authentication

πŸ‘€Ankit desai

3πŸ‘

Also make sure, is_active = True for the user object that you are saving in your serializer, because

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['fullname', 'username', 'email', 'password']


    def create(self, validated_data):
        password = validated_data.pop('password', None)
        instance = self.Meta.model(**validated_data)
        
        # Adding the below line made it work for me.
        instance.is_active = True
        if password is not None:
            # Set password does the hash, so you don't need to call make_password 
            instance.set_password(password)
        instance.save()
        return instance

Note ( As per docs )

The login_required decorator does NOT check the is_active flag on a user, but the default AUTHENTICATION_BACKENDS reject inactive users.

1πŸ‘

Did you remember to set in settings:

AUTH_USER_MODEL = 'your_app_name.User'
πŸ‘€Leoog

1πŸ‘

It seems my error was being caused by a write_only parameter on my password field

class RegisterSerializer(serializers.ModelSerializer):
    password = serializers.CharField(
        max_length=68, min_length=6, write_only=True)


    class Meta:
        model = User
        fields = ['email', 'username', 'password']

Removed it:

class RegisterSerializer(serializers.ModelSerializer):
    password = serializers.CharField(
        max_length=68, min_length=6)


    class Meta:
        model = User
        fields = ['email', 'username', 'password']

and then it was all sunshine and rainbows after that πŸ™‚

πŸ‘€apiyo

0πŸ‘

try this

from django.contrib.auth.hashers import make_password

class UserManager(BaseUserManager):
def create_user(self, email, username, password, alias=None):
    user = self.model(
    email = self.normalize_email(email),
            username = username,)
    user.set_password(make_password(password))
    user.save()
    return user
def create_superuser(self, email, username, password):
   self.create_user(email, username, password)
   user.is_staff()
   user.is_superuser = True
   user.save()
   return user
πŸ‘€neomatrixcode

0πŸ‘

Downgraded manually from PyJWT==2.0.0 to PyJWT==1.7.1 and solved our problem

pip install PyJWT==1.7.1

We are using djangorestframework==3.12.1 and djangorestframework-simplejwt==4.4.0 on our requirements.txt and that gave us automatically the 2.0.0 version dependency.

πŸ‘€movie maina

0πŸ‘

In my opinion, there is a problem where an email address and username is provided for the serializer, but an email is expected as a username for authentication.
I had the same error too. I also made some kind of custom user and tried to login to get a couple of json web tokens. But I only used email, not username. So what I did in the end and it works for me. Perhaps this example explains something in the place where authentication is done …

Model and manager like this:

from django.db import models
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import (
    AbstractBaseUser, 
    BaseUserManager
    )
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist
from rest_framework_simplejwt.tokens import RefreshToken


class CustomUserManager(BaseUserManager):
    def get_or_create(self, email=None, **kwargs):
        allowed_kwargs = ['first_name', 'last_name', 'img', 'about']
        if email is not None:
            try:
                user_obj = super(CustomUserManager, self).get(email=email)
                if kwargs:
                    for k, v in kwargs.items():
                        setattr(user_obj, k, v)
                    user_obj.save()
            except ObjectDoesNotExist:
                email = self.normalize_email(email)
                user_obj = self.model(email=email)
                password = kwargs.pop('password', None)
                if password is not None:
                    user_obj.set_password(password)
                if kwargs:
                    for k, v in kwargs.items():
                        if k in allowed_kwargs:
                            setattr(user_obj, k, v)
                user_obj.save()
        else:
            return False
        return user_obj
        

class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), null=True, unique=True)
    first_name = models.CharField(max_length=150, null=True, blank=True)
    last_name = models.CharField(max_length=150, null=True, blank=True)
    img = models.URLField(null=True, blank=True)
    about = models.TextField(_('about'), max_length=500, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __str__(self):
        return f'<CustomUser(id={self.id} - email={self.email})>'

    class Meta:
        ordering = ['-created_at']
    
    @property
    def full_name(self):
        if hasattr(self, 'first_name') and hasattr(self, 'last_name'):
            return f'{self.first_name} {self.last_name}'
        return 'No fullname'

    @property
    def jwt_tokens(self):
        refresh = RefreshToken.for_user(self)
        return {
            'refresh': str(refresh),
            'access': str(refresh.access_token),
        }

Customizing token serializer:

from django.contrib.auth import authenticate
from django.contrib.auth.models import update_last_login
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import serializers

class CustomTokenSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    email = serializers.EmailField(required=True)
    password = serializers.CharField(min_length=8, write_only=True)
    def validate(self, email, password):
        try:
            self.user = CustomUser.objects.get(email=email)
        except ObjectDoesNotExist as e:
            message = {'error': f'User with email={email} does not exist.'}
            return message
        check_auth = authenticate(username=email, password=password)
        if check_auth is None:
            message = {'error': 
                       'The user exists, but the password is incorrect.'}
            return message
        data = self.user.jwt_tokens
        update_last_login(None, self.user)
        return data

Urls:

urlpatterns += [
    path('login/token/', views.LoginTokenView.as_view(), name='login-token')
]

0πŸ‘

I faced a similar issue. Turns out that I had not included the password field among the fields in the Writer serializer.

Before I had code like this;

class UserWriterSerializer(serializers.ModelSerializer):
    class Meta:
        model = AppUser
        fields = [
            'id',
            'username',
            'first_name',
            'last_name',
            'email',
            'is_active',
            'telephone',
            'userType',
            'gender',
            'registration_date'
        ]

Then I added the password field to have this;

class UserWriterSerializer(serializers.ModelSerializer):
    class Meta:
        model = AppUser
        fields = [
            'id',
            'username',
            'password',
            'first_name',
            'last_name',
            'email',
            'is_active',
            'telephone',
            'userType',
            'gender',
            'registration_date'
        ]

So in summary, some different value was being saved in the database which was not the password I added. Thus having that error, because the password you place as an input is not matching with what is in the database. Make sure that the serializer is correct

πŸ‘€Okello Marvin

Leave a comment