Magic Links¶
Setup¶
- Configure email settings if you haven't already.
- 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:
- Creates or retrieves a user account
- Generates a secure login link
- 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
:
After authentication, users are redirected to LOGIN_REDIRECT_URL
(configured in config/settings.py
).