I'm writing a custom backend for authenticating users by their email address instead of username. I've already written a custom User model that inherits from AbstractUser
:
class User(AbstractUser):
email = models.EmailField(
_('email address'),
max_length=150,
unique=True,
primary_key=True,
help_text=_('Required. 150 characters of fewer. Must be a valid email address.'),
error_messages={
'unique':_("A user with that email address already exists."),
},
)
As you can see, I've made the email-field unique, and also, a primary key.
I'm following the docs for how to write the backend, and I've read this:
The Django admin is tightly coupled to the Django User object. The best way to deal with this is to create a Django User object for each user that exists for your backend (e.g., in your LDAP directory, your external SQL database, etc.) You can either write a script to do this in advance, or your authenticate method can do it the first time a user logs in.
It says that I should create a Django User object (which I'm guessing is the default User object django.contrib.auth.models.User) for each user in my backend.
Question 1: If a backend is a class that implements get_user(user_id)
and authenticate(request, **credentials)
(this is said in the docs), how can a backend contain users? What is meant by "user in my backend"?
Question 2: Do I really have to create a normal Django User object, if my cutsom user objects are subclasses of AbstractUser
? They're pretty much identical to django.contrib.auth.models.User
, except that the email-field is primary key and unique.