0
votes

I'm using django rest auth for auth in my rest api. I use custom user model with no username field. So, I've to use custom login serializer. But when I log in, django rest auth says "userprofile with this email address already exists.". How to solve this problem?

my settings:

REST_USE_JWT = True
REST_AUTH_SERIALIZERS = {
    'LOGIN_SERIALIZER': 'accounts.api.serializers.RestAuthLoginSerializer',
    'REGISTER_SERIALIZER': 'accounts.api.serializers.RestAuthRegisterSerializer',
}
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email'
ACCOUNT_USERNAME_REQUIRED = False

serializers.py

from rest_framework import serializers

from accounts.models import UserProfile


class RestAuthLoginSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('email', 'password')


class RestAuthRegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('email', 'password', 'first_name', 'last_name', 'business_name', 'is_business_account')

models.py

class UserProfile(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True)
    avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
    # business profile related data
    business_name = models.CharField(_('business name'), max_length=30, blank=True, null=True)
    business_phone = models.CharField(_('business phone'), max_length=20, blank=True, null=True)
    is_business_account = models.BooleanField(verbose_name=_("is business account"), default=False)

    date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
    is_active = models.BooleanField(_('active'), default=True)
    is_admin = models.BooleanField(_('staff status'), default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
1
can you please update with your post's your urls.py, possible reason you are misplaced login or signup process.Shakil

1 Answers

0
votes

I found the problem and fixed it myself. I just had to add

AUTHENTICATION_BACKENDS = (
   "django.contrib.auth.backends.ModelBackend",
   "allauth.account.auth_backends.AuthenticationBackend"
)

to settings.py file.