An extension to django.contrib.auth
that replaces username-based authentication
with email-based authentication, without requiring any changes to existing
applications that depend on the django.contrib.auth.User
model.
- Add
emailauth
to yourINSTALLED_APPS
in your Django settings file. - Replace
django.contrib.backends.ModelBackend
in theAUTHENTICATION_BACKENDS
setting withemailauth.backends.ModelBackend
. It's not recommended that you use both the built-inModelBackend
and the replacementModelBackend
at the same time (although it it technically possible to do so.) - Replace the following built-in forms and views:
django.contrib.auth.views.login
withemailauth.views.login
django.contrib.auth.views.password_reset
withemailauth.views.password_reset
django.contrib.auth.forms.AuthenticationForm
withemailauth.forms.AuthenticationForm
django.contrib.auth.forms.UserCreationForm
withemailauth.forms.UserCreationForm
django.contrib.auth.forms.UserChangeForm
withemailauth.forms.UserChangeForm
django.contrib.auth.forms.PasswordResetForm
withemailauth.forms.PasswordResetForm
django.contrib.auth.forms.AdminAuthenticationForm
withemailauth.forms.AdminAuthenticationForm
- If you use a custom
AdminSite
instance, modify it to match the implementation inemailauth.admin
. - If you have modified the default
registration/password_reset_email.html
template, update it to match the template provided withemailauth
. (If you have not modified it, no action is necessary.) - Make sure that you're not using
username
anywhere in your application.
This is not simply a drop-in application. This application monkey patches
the buit-in User
model in several ways that could break existing applications,
and will at least require a database migration on existing projects. These include:
- Changes to the
User
model:- The
username
field hasmax_length=254
. (This is modifiable bysettings.MAXIMUM_USERNAME_LENGTH
, which is the same assettings.MAXIMUM_EMAIL_LENGTH
, unless otherwise specified.) - The
email
field now has these changed attributes:blank=False
null=False
unique=True
max_length=254
(This is modifiable bysettings.MAXIMUM_EMAIL_LENGTH
)
- The
- The addition a signal to ensure that
User.username
is a mirror ofUser.email
(This may interact in unexpected/catastrophic ways with existing data!)
This is basically one big hack on top of the built-in auth
module to coerce it
into behaving in ways it was not designed to behave by using dirty hacks and
abusing the private Model API. This is not guaranteed to continue to work with
any version of Django in the future (or any current or previous versions, for
that matter.)