社交用户的用户名和电子邮件互换
username and email interchanged for social user
我正在使用 google oauth2 进行社交身份验证,我获取了用户的用户名和电子邮件并将其保存在我的模型中,但是用户名和电子邮件字段正在交换。是什么导致了这样的问题?
models.py
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=255, db_index=True)
email = models.EmailField(max_length=255, unique=True, db_index=True)
is_verified = models.BooleanField(default=False)
# is_authenticated = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
auth_provider = models.CharField(max_length=225, blank=False, null=False, default=AUTH_PROVIDERS.get('email'))
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = UserManager()
def __str__(self):
return self.email
def tokens(self):
refresh = RefreshToken.for_user(self)
return {
'refresh': str(refresh),
'access': str(refresh.access_token)
}
register.py
def register_social_user(provider, user_id, email, name):
filtered_user_by_email = User.objects.filter(email=email)
if filtered_user_by_email.exists():
if provider == filtered_user_by_email[0].auth_provider:
registered_user = authenticate(
email=email, password=os.environ.get('SOCIAL_SECRET'))
return {
'username': registered_user.username,
'email': registered_user.email,
'tokens': registered_user.tokens()}
else:
raise AuthenticationFailed(
detail='Please continue your login using ' + filtered_user_by_email[0].auth_provider)
else:
user = {
'username': generate_username(name), 'email': email,
'password': os.environ.get('SOCIAL_SECRET')}
user = User.objects.create_user(**user)
user.is_verified = True
user.auth_provider = provider
user.save()
print('user',user)
new_user = authenticate(
email=email, password=os.environ.get('SOCIAL_SECRET'))
print ('new:',new_user)
return {
'id':user.id,
'email': user.email,
'username': user.username,
'tokens': user.tokens()
}
这里是 link 的完整代码:https://github.com/abinashkarki/rest_framework_authentication
我 运行 createsuperuser
命令,我看到它将电子邮件值保存到用户名字段中,反之亦然。
我在您的自定义 UserManager
中发现了问题,您在 UserManager.create_user
方法中写错了用户名和电子邮件字段的顺序:
def create_user(self, username, email, password=None, **extra_fields):
return self._create_user(email, username, password, **extra_fields)
def create_superuser(self, username, email, password=None, **extra_fields):
user = self.create_user(username, email, password)
user.is_superuser = True
user.is_staff = True
user.has_delete_permission = True
user.save()
return user
你应该做的基本上是像这样修复 create_user
方法:
def create_user(self, username, email, password=None, **extra_fields):
return self._create_user(username, email, password, **extra_fields)
现在应该可以了
我正在使用 google oauth2 进行社交身份验证,我获取了用户的用户名和电子邮件并将其保存在我的模型中,但是用户名和电子邮件字段正在交换。是什么导致了这样的问题?
models.py
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=255, db_index=True)
email = models.EmailField(max_length=255, unique=True, db_index=True)
is_verified = models.BooleanField(default=False)
# is_authenticated = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
auth_provider = models.CharField(max_length=225, blank=False, null=False, default=AUTH_PROVIDERS.get('email'))
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = UserManager()
def __str__(self):
return self.email
def tokens(self):
refresh = RefreshToken.for_user(self)
return {
'refresh': str(refresh),
'access': str(refresh.access_token)
}
register.py
def register_social_user(provider, user_id, email, name):
filtered_user_by_email = User.objects.filter(email=email)
if filtered_user_by_email.exists():
if provider == filtered_user_by_email[0].auth_provider:
registered_user = authenticate(
email=email, password=os.environ.get('SOCIAL_SECRET'))
return {
'username': registered_user.username,
'email': registered_user.email,
'tokens': registered_user.tokens()}
else:
raise AuthenticationFailed(
detail='Please continue your login using ' + filtered_user_by_email[0].auth_provider)
else:
user = {
'username': generate_username(name), 'email': email,
'password': os.environ.get('SOCIAL_SECRET')}
user = User.objects.create_user(**user)
user.is_verified = True
user.auth_provider = provider
user.save()
print('user',user)
new_user = authenticate(
email=email, password=os.environ.get('SOCIAL_SECRET'))
print ('new:',new_user)
return {
'id':user.id,
'email': user.email,
'username': user.username,
'tokens': user.tokens()
}
这里是 link 的完整代码:https://github.com/abinashkarki/rest_framework_authentication
我 运行 createsuperuser
命令,我看到它将电子邮件值保存到用户名字段中,反之亦然。
我在您的自定义 UserManager
中发现了问题,您在 UserManager.create_user
方法中写错了用户名和电子邮件字段的顺序:
def create_user(self, username, email, password=None, **extra_fields):
return self._create_user(email, username, password, **extra_fields)
def create_superuser(self, username, email, password=None, **extra_fields):
user = self.create_user(username, email, password)
user.is_superuser = True
user.is_staff = True
user.has_delete_permission = True
user.save()
return user
你应该做的基本上是像这样修复 create_user
方法:
def create_user(self, username, email, password=None, **extra_fields):
return self._create_user(username, email, password, **extra_fields)
现在应该可以了