Как я могу изменить мутацию регистра по умолчанию в django-graphql-auth?
Я хочу изменить поля мутации регистра, например, я хочу запрашивать пароль только один раз, а также я хочу добавить дополнительные поля, такие как имя_первое, имя_последнее, дата_рождения ... все это, но все же я хочу извлечь выгоду из использования проверки электронной почты и всех хороших частей, которые djang-graphql-auth приносит .
Вот что у меня сейчас есть :
- Пользовательский пользователь (я хочу, чтобы пользователь мог войти в систему только с помощью электронной почты) :
from django.db import models
from django.contrib.auth.models import AbstractUser
class ExtendUser(AbstractUser):
email = models.EmailField(blank=False, max_length=255, unique=True, verbose_name="email")
date_of_birth = models.DateField(blank=False, null=True, verbose_name="date_of_birth")
# phone =
# country =
city = models.CharField(blank=True, max_length=150, verbose_name="city")
company_name = models.CharField(blank=False, max_length=150,
verbose_name="company_name")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# USERNAME_FIELD = "username"
EMAIL_FIELD = "email
- В моем schema.py у меня есть следующее :
import graphene from graphene_django import DjangoObjectType from .models import ExtendUser from graphql_auth import mutations from graphql_auth.schema import UserQuery, MeQuery class UserType(DjangoObjectType): class Meta: model = ExtendUser fields = ("id", "first_name", "last_name", "email", "date_of_birth", "company_name", "city") class CreateUser(graphene.Mutation): user = graphene.Field(UserType) class Arguments: first_name = graphene.String(required=True) last_name = graphene.String(required=True) # username = graphene.String(required=True) email = graphene.String(required=True) password = graphene.String(required=True) date_of_birth = graphene.Date(required=True) city = graphene.String(required=True) company_name = graphene.String(required=True) def mutate(self, info, first_name, last_name, password, email, date_of_birth, city, company_name): user = ExtendUser ( email = email, first_name = first_name, last_name = last_name, date_of_birth = date_of_birth, city = city, company_name= company_name, ) user.set_password(password) user.save() return CreateUser(user=user) class AuthMutation(graphene.ObjectType): # register = mutations.Register.Field() register = CreateUser.Field() verify_account = mutations.VerifyAccount.Field() token_auth = mutations.ObtainJSONWebToken.Field() update_account = mutations.UpdateAccount.Field() resend_activation_email = mutations.ResendActivationEmail.Field() send_password_reset_email = mutations.SendPasswordResetEmail.Field() password_reset = mutations.PasswordReset.Field() class RegisterUserQuery(graphene.ObjectType): user_details = graphene.Field(UserType) users = graphene.List(UserType) def resolve_users(root, info): user = info.context.user if not user.is_authenticated: raise Exception("Authentication credentials were not provided") return ExtendUser.objects.all() def resolve_user_details(root, info , **kwargs): user = info.context.user if not user.is_authenticated: raise Exception('Not logged in!') return ExtendUser.objects.get(email=user) class Query(RegisterUserQuery,UserQuery, MeQuery, graphene.ObjectType): pass class Mutation(AuthMutation, graphene.ObjectType): pass schema = graphene.Schema(query=Query, mutation=Mutation)
Но способ, который я пробовал в коде выше, больше не отправляет проверку электронной почты, как я могу решить это, имея все проверки и все, что приносит graphql-auth, но иметь пользовательскую мутацию регистра?
Я решил эту проблему следующим образом: В вашем User models.py:
from django.contrib.auth.models import (AbstractUser, BaseUserManager, )
from django.db import models
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError("The given email must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
username = None
email = models.EmailField('email address', unique=True)
date_of_birth = models.DateField(blank=True, null=True)
city = models.CharField(max_length=250,blank=True, null=True)
company_name = models.CharField(max_length=250,blank=True, null=True)
USERNAME_FIELD = 'email' #This is the modern way, where email is the username
REQUIRED_FIELDS = []
objects = UserManager()
В вашем settings.py:
AUTH_USER_MODEL = "account.User" #My User models is in the account app: account.models.User
GRAPHQL_AUTH = {
'USERNAME_FIELD': ['email'], # Same as USERNAME_FIELD in User model
'LOGIN_ALLOWED_FIELDS': ['email'],
'USER_NODE_FILTER_FIELDS': {
"email": ["exact", ],
"is_active": ["exact"],
"status__archived": ["exact"],
"status__verified": ["exact"],
"status__secondary_email": ["exact"],
},
'REGISTER_MUTATION_FIELDS': ['email', 'first_name'], #extend with your fields: city, date_of_birth, company
'REGISTER_MUTATION_FIELDS_OPTIONAL': ['email'],
'EMAIL_TEMPLATE_PASSWORD_RESET': "email/account_password_reset_email.html", # My modified template route
'EMAIL_TEMPLATE_ACTIVATION': "email/account_activation_email.html", # My modified template route
'EMAIL_SUBJECT_ACTIVATION': "email/account_activation_subject.txt", # My modified subject route
'EMAIL_SUBJECT_PASSWORD_RESET': "email/account_password_reset_subject.txt", # My modified subject route
"SEND_ACTIVATION_EMAIL": SEND_ACTIVATION_EMAIL, #A boolean parameter if you want activation or not
"ACTIVATION_PATH_ON_EMAIL": config('ACTIVATION_PATH_ON_EMAIL', default="account/activate"),
"PASSWORD_RESET_PATH_ON_EMAIL": config('PASSWORD_RESET_PATH_ON_EMAIL', default="account/password-reset"),
"ALLOW_LOGIN_NOT_VERIFIED": config('ALLOW_LOGIN_NOT_VERIFIED', default=False),
"EXPIRATION_PASSWORD_RESET_TOKEN": config('EXPIRATION_PASSWORD_RESET_TOKEN', default=timedelta(hours=1)),
}