"CustomUser has no customer" error for users signed in with google
I have a CustomUser model to differentiate between customer and merchant.
class CustomUser(AbstractUser):
is_customer = models.BooleanField(default=False)
is_merchant = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
Now the customers have the add to cart functionality for which I have a utils.py
def cartData(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
else:
cookieData = cookieCart(request)
cartItems = cookieData['cartItems']
order = cookieData['order']
items = cookieData['items']
return {'cartItems':cartItems ,'order':order, 'items':items}
As per the utils.py I always need request.user.customer. The customer instance here is created during customer registration.
forms.py:
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_customer = True
user.first_name = self.cleaned_data.get('first_name')
user.last_name = self.cleaned_data.get('last_name')
user.username=self.cleaned_data.get('username')
user.email=self.cleaned_data.get('email')
user.phone_number=self.cleaned_data.get('phone_number')
user.address=self.cleaned_data.get('address')
user.save()
customer = Customer.objects.create(user=user)
customer.first_name=self.cleaned_data.get('first_name')
customer.last_name=self.cleaned_data.get('last_name')
customer.username=self.cleaned_data.get('username')
customer.email=self.cleaned_data.get('email')
customer.phone_number=self.cleaned_data.get('phone_number')
customer.address=self.cleaned_data.get('address')
customer.save()
return user
So with the customer, everything worked just fine. But the problem arises when I try to sign in with google. I have signed in with google functionality with Oauth, worked on the google developers console, installed required packeges, and made changes to settings.py.
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'projectapp.apps.ProjectappConfig',
'django_extensions',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
]
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
'AUTH_PARAMS': {
'access_type': 'online',
}
}
}
So the error is when I clicked the button "sign in with google" I encountered the error
CustomUser has no customer.
In the admin panel, a user is created but there is no customer for that user.
Obviously, I realize from the error that for the users who are signed in with google need a customer instance. But I don't realize how I could create them as I don't have a particular form for this type of user.