Customizing

Django Mail Auth can be easily extend. Besides template adaptations it is possible to send send different messages like SMS. To make those changes you will need to write a custom login form.

Custom login form

Custom login forms need to inherit from BaseLoginForm and override the save method.

The following example is for a login SMS via twilio. This will require a custom user model with a unique phone_number field:

from django import forms
from django.contrib.auth import get_user_model
from django.template import loader
from mailauth.forms import BaseLoginForm


class SmsLoginForm(BaseLoginForm):
    phone_number = forms.CharField()

    template_name = 'registration/login_sms.txt
    from_number = None

    def __init__(self, *args, **kwargs):
        self.twilio_client = TwilioRestClient(
            settings.TWILIO_SID,
            settings.TWILIO_AUTH_TOKEN
        )
        super().__init__(*args, **kwargs)

    def save(self):
        phone_number = self.cleaned_data['phone_number']
        user = get_user_model().objects.get(
            phone_number=phone_number
        )
        context = self.get_context(self.request, user)

        from_number = self.from_number or getattr(
            settings, 'DEFAULT_FROM_NUMBER'
        )
        sms_content = loader.render_to_string(
            self.template_name, context
        )

        self.twilio_client.messages.create(
            to=user.phone_number,
            from_=from_number,
            body=sms_content
        )

To add the new login form, simply add a new login view to your URL config with the custom form:

from django.urls import path
from mailauth.views import LoginView

from .forms import SmsLoginForm

urlpatterns = [
    path(
        'login/sms/',
        LoginView.as_view(form_class=SmsLoginForm),
        name='login-sms'
    ),
]

API documentation

class mailauth.forms.BaseLoginForm(data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=<class 'django.forms.utils.ErrorList'>, label_suffix=None, empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None)
get_context(request, user)

Return the context for a message template render.

Parameters:
  • request (django.http.request.HttpRequest) – Current request.
  • user – The user requesting a login message.
Returns:

A context dictionary including:

  • site
  • site_name
  • token
  • login_url
  • user

Return type:

dict

get_login_url(request, token, next=None)

Return user login URL including the access token.

Parameters:
  • request (django.http.request.HttpRequest) – Current request.
  • token (str) – The user specific authentication token.
  • next (str) – The path the user should be forwarded to after login.
Returns:

User login URL including the access token.

Return type:

str

get_token(user)

Return the access token.

save()

Send login URL to users.

Called from the login view, if the form is valid.

This method must be implemented by subclasses. This method should trigger the login url to be sent to the user.