Skip to main content

A Django app for passwordless authentication using magic links.

Project description

django-solomon

PyPI version Python versions Django versions Documentation Status

A Django app for passwordless authentication using magic links.

Features

  • Passwordless authentication using magic links sent via email
  • Configurable link expiration time
  • Blacklist functionality to block specific email addresses
  • Support for auto-creating users when they request a magic link
  • Customizable templates for emails and pages
  • Compatible with Django's authentication system

Installation

pip install django-solomon

Configuration

  1. Add django_solomon to your INSTALLED_APPS in your Django settings:
INSTALLED_APPS = [
    # ...
    'django_solomon',
    # ...
]
  1. Add the authentication backend to your settings:
AUTHENTICATION_BACKENDS = [
    'django_solomon.backends.MagicLinkBackend',
    'django.contrib.auth.backends.ModelBackend',  # Keep the default backend
]
  1. Include the django-solomon URLs in your project's urls.py:
from django.urls import include, path

urlpatterns = [
    # ...
    path('auth/', include('django_solomon.urls')),
    # ...
]
  1. Set the login URL in your settings to use django-solomon's login view:
LOGIN_URL = 'django_solomon:login'

This ensures that when users need to authenticate, they'll be redirected to the magic link login page.

  1. Configure your email settings to ensure emails can be sent:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.example.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@example.com'
EMAIL_HOST_PASSWORD = 'your-password'
DEFAULT_FROM_EMAIL = 'your-email@example.com'

Settings

django-solomon provides several settings that you can customize in your Django settings file:

Setting Default Description
SOLOMON_LINK_EXPIRATION 300 The expiration time for magic links in seconds
SOLOMON_ONLY_ONE_LINK_ALLOWED True If enabled, only one active magic link is allowed per user
SOLOMON_CREATE_USER_IF_NOT_FOUND False If enabled, creates a new user when a magic link is requested for a non-existent email
SOLOMON_LOGIN_REDIRECT_URL settings.LOGIN_REDIRECT_URL The URL to redirect to after successful authentication
SOLOMON_ALLOW_ADMIN_LOGIN True If enabled, allows superusers to log in using magic links
SOLOMON_ALLOW_STAFF_LOGIN True If enabled, allows staff users to log in using magic links
SOLOMON_MAIL_TEXT_TEMPLATE "django_solomon/email/magic_link.txt" The template to use for plain text magic link emails
SOLOMON_MAIL_MJML_TEMPLATE "django_solomon/email/magic_link.mjml" The template to use for HTML magic link emails (MJML format)
SOLOMON_LOGIN_FORM_TEMPLATE "django_solomon/base/login_form.html" The template to use for the login form page
SOLOMON_INVALID_MAGIC_LINK_TEMPLATE "django_solomon/base/invalid_magic_link.html" The template to use for the invalid magic link page
SOLOMON_MAGIC_LINK_SENT_TEMPLATE "django_solomon/base/magic_link_sent.html" The template to use for the magic link sent confirmation page

Usage

Basic Usage

  1. Direct users to the magic link request page at /auth/magic-link/
  2. Users enter their email address
  3. A magic link is sent to their email
  4. Users click the link in their email
  5. They are authenticated and redirected to the success URL

Template Customization

You can override the default templates by creating your own versions in your project:

  • django_solomon/login_form.html - The form to request a magic link
  • django_solomon/magic_link_sent.html - The confirmation page after a magic link is sent
  • django_solomon/invalid_magic_link.html - The error page for invalid magic links
  • django_solomon/email/magic_link.txt - The plain text email template for the magic link
  • django_solomon/email/magic_link.mjml - The HTML email template for the magic link (in MJML format)

Alternatively, you can specify custom templates using the settings variables:

  • SOLOMON_LOGIN_FORM_TEMPLATE - Custom template for the login form
  • SOLOMON_MAGIC_LINK_SENT_TEMPLATE - Custom template for the confirmation page
  • SOLOMON_INVALID_MAGIC_LINK_TEMPLATE - Custom template for the error page
  • SOLOMON_MAIL_TEXT_TEMPLATE - Custom template for the plain text email
  • SOLOMON_MAIL_MJML_TEMPLATE - Custom template for the HTML email (MJML format)

Programmatic Usage

You can also use django-solomon programmatically in your views:

from django.contrib.auth import authenticate, login
from django_solomon.models import MagicLink

# Create a magic link for a user
magic_link = MagicLink.objects.create_for_user(user)

# Authenticate a user with a token
user = authenticate(request=request, token=token)
if user:
    login(request, user)

License

This software is licensed under MIT license.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request on Codeberg.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_solomon-0.1.3.tar.gz (42.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_solomon-0.1.3-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file django_solomon-0.1.3.tar.gz.

File metadata

  • Download URL: django_solomon-0.1.3.tar.gz
  • Upload date:
  • Size: 42.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for django_solomon-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8c1745a453c549707159446c8c595d6423e6922b856609bd39a7ade6438c36f5
MD5 6103643817f1b9da38378a5ec50d7b62
BLAKE2b-256 653da99740db61aaa8d730e5350813b1ba6045bfedf72b4fea22edf9c0efa634

See more details on using hashes here.

File details

Details for the file django_solomon-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_solomon-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 18f858bd935a9595d8fd965238525214d64075c89e152f63bb113c4750fb7702
MD5 5045f35fb29ae9f1f034cda933cdae4b
BLAKE2b-256 bea45b0d7e257c52bc4452664ae7b5e46b7a549645814725b1524510a3a6973f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page