Problem in getting user email in django app with sicial auth
hello i have a django e commerce app that have google social auth when new users create an account we create a some % of dicount code for them i have UserIdentifier class that when users create new account i save either their phoen number or their email address to then when a new users create account i check if we have this users before or not to stop them from abusing the discount code i have this pipeline :
from django.contrib.auth import get_user_model, login
from django.shortcuts import redirect
from .models import Profile,UserIdentifier
import re
User = get_user_model()
def save_profile(backend, user, response, *args, **kwargs):
if backend.name == 'google-oauth2':
email = response.get('email')
first_name = response.get('given_name')
last_name = response.get('family_name')
unique_identifier = email
user_exists_before = UserIdentifier.objects.filter(identifier=unique_identifier).exists()
if not user_exists_before:
UserIdentifier.objects.create(identifier=unique_identifier)
# Update user fields
if email:
user.email = email
if user.phone_number and not re.match(r'^\d{11}$', user.phone_number):
user.phone_number = None
user.first_name = first_name
user.last_name = last_name
user.save()
# Update or create profile
profile, created = Profile.objects.get_or_create(user=user)
profile.first_name = first_name
profile.last_name = last_name
profile.email = email
profile.save()
# Handle login and redirection
request = kwargs.get('request') # Get the request object from kwargs
if request:
user.backend = 'social_core.backends.google.GoogleOAuth2'
login(request, user) # Log the user in regardless of whether they're new
if user.is_new and not user_exists_before:
user.is_new = False # Mark the user as no longer new
user.save()
return redirect("accounts:welcome")
elif user.is_new and user_exists_before:
user.is_new = False
user.save()
# Redirect for existing users
return redirect("pages:index")
and this is my Customuser model :
class CustomUserManager(BaseUserManager):
def create_user(self, phone_number, password=None, **extra_fields):
email = extra_fields.get('email')
if email:
try:
existing_user = CustomUser.objects.get(email=email)
return existing_user
except CustomUser.DoesNotExist:
pass
if not phone_number:
raise ValueError('The Phone Number must be set')
user = self.model(phone_number=phone_number, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, phone_number, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(phone_number, password, **extra_fields)
class CustomUser(AbstractBaseUser, PermissionsMixin):
phone_number = models.CharField(max_length=15, unique=True, null=True, blank=True)
email = models.EmailField(unique=True, null=True, blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_new = models.BooleanField(default=True)
score = models.PositiveIntegerField(default=10, null=True, blank=True)
objects = CustomUserManager()
USERNAME_FIELD = 'phone_number'
REQUIRED_FIELDS = []
def save(self, *args, **kwargs):
is_new_user = self.pk is None
super().save(*args, **kwargs)
if is_new_user:
self.generate_referral_code()
identifier = self.phone_number if self.phone_number else self.email
print(identifier)
if not UserIdentifier.objects.filter(identifier=identifier).exists():
DiscountCodes.objects.create(user=self)
Profile.objects.get_or_create(user=self)
that print(identifier) when they log with gmail account return for etc its my email account pouryamohamadi35@gmail.com but it return pouryamohamadi3 in database user account have correct email and also UserIdentifier have correct email where is the problem and how can fix it ?