Search code examples
python-3.xdjangodjango-3.0django-4.0

django social all auth - multiple user type


I have two Registration pages: Register as Buyer, Register as Seller.

on both Pages , I've included Normal Registration and social authentication using Google and Facebook.

I've added social authentication using Django all-auth, but cannot get user_role.

[1][Registration Page]:

  • after Registration : user is redirected to home page and as there is no user_role set in social authentication , one cannot access Buyer or Seller 's section.

[2][Signup with Google]: After signup : User is registered as Blank Login.

  • check home page after Google signup:

MY TASK : Is to assign user_type on both the buttons: Buyer and Seller. on click button redirect user to its particular Registration Page and Login/Signup with Social Authentication and after Successful-Login Redirect User to its Page and show user_type in Django-administration-panel as:

User_type | Email | is_active

Buyer | [email protected] | True

Seller | [email protected] | False


Solution

  • You can create a profile model for all users and then give the user a profile like this.

    #models.py

    class Profile(models.Model):
        user_type = models.CharField()
        user = models.ForeignKey(User, related_name="profile")
    

    this will run after the user has created.

    #signals.py

    from django.db.models.signals import post_save
    from django.dispatch import receiver
    from . models import Profile
    from django.conf import settings
    
    User = settings.AUTH_USER_MODEL
    
    @receiver(post_save, sender=User)
    def create_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance, user_type=instance.user_type)
    

    edit: I don't know how is your user_type field I assume it is "user_type"