Skip to content

Magic Links

Setup

  1. Configure email settings if you haven't already.
  2. Magic Links functionality is enabled by default.

Implementation Details

Magic link authentication is handled by django-sesame and implemented in the login view (user/views.py). The process:

  1. Creates or retrieves a user account
  2. Generates a secure login link
  3. Emails the link to the user
user/views.py
def login(request):
    # ...
        if form.is_valid():
            email = form.cleaned_data['email']
            user, _ = User.objects.get_or_create(email=email)
            link = reverse('user:sesame-login')
            link = request.build_absolute_uri(link)
            link += sesame.utils.get_query_string(user)

            user.email_user(
                subject='Your login link',
                message=f'Click the link to log in: {link}',
            )
    # ...

The login URL is configured in user/urls.py:

user/urls.py
path('login/auth/', LoginView.as_view(), name='sesame-login'),

After authentication, users are redirected to LOGIN_REDIRECT_URL (configured in config/settings.py).