Skip to main content

A simple, customizable Django authentication package with JWT support, email verification, and password management

Project description

Django Simple Auth

A simple, customizable, and feature-rich authentication package for Django. It provides a complete solution for user registration, login, password management, and email verification, with support for custom user models and JWT authentication.

Table of Contents

  1. Features
  2. Installation
  3. Quick Start
  4. Configuration Reference
  5. Custom User Model Support
  6. API Documentation
  7. Template Customization
  8. Email Verification

Features

  • Flexible Login: Log in with Username, Email, or Phone Number.
  • Custom User Model Support: Seamlessly works with custom user models and extra fields (e.g., Avatar, Address).
  • JWT Authentication: Built-in API endpoints for modern frontend integration.
  • Email Verification: Integrated support for Resend to verify user emails.
  • Password Management: Ready-to-use views for Password Reset and Password Change.
  • Customizable: Override templates and forms easily.
  • Beginner-Friendly: Clear error messages, helpful hints, and comprehensive documentation.

Quick Start

New to Django Simple Auth? Start here! 👉 QUICKSTART.md

Or run the setup checker:

python manage.py simpleauth_check

Installation

  1. Install via pip:

    pip install django-simple-auth-pkg
    

    (For local development, navigate to the package directory and run pip install -e .)

  2. Add to INSTALLED_APPS: Open your project's settings.py and add:

    INSTALLED_APPS = [
        # ... default django apps ...
        'rest_framework',
        'rest_framework_simplejwt',
        'simple_auth',
        # 'accounts', # Your custom app if you have one
    ]
    
  3. Include URLs: Open your project's urls.py and add:

    from django.urls import path, include
    
    urlpatterns = [
        # ...
        path('auth/', include('simple_auth.urls')),
    ]
    
  4. Run Migrations:

    python manage.py migrate
    

Quick Start

Once installed, you can access the following views immediately:

  • Signup: /auth/signup/
  • Login: /auth/login/
  • Logout: /auth/logout/
  • Password Change: /auth/password-change/
  • Password Reset: /auth/password-reset/

Configuration Reference

Add these settings to your settings.py to control the package behavior.

Setting Default Description
SIMPLE_AUTH_LOGIN_FIELD 'username' Field used for login. Options: 'username', 'email', 'phone_number'.
SIMPLE_AUTH_USER_FIELDS [] List of extra fields to include in the Signup form (e.g., ['first_name', 'avatar']).
SIMPLE_AUTH_USE_JWT False Set to True to enable API endpoints for JWT authentication.
SIMPLE_AUTH_EMAIL_VERIFICATION False Set to True to require email verification.
RESEND_API_KEY None Your Resend API Key (required if email verification is on).
LOGIN_REDIRECT_URL '/accounts/profile/' URL to redirect to after successful login.
LOGOUT_REDIRECT_URL None URL to redirect to after logout.

Example Configuration:

SIMPLE_AUTH_LOGIN_FIELD = 'email'
SIMPLE_AUTH_USER_FIELDS = ['first_name', 'last_name', 'avatar']
SIMPLE_AUTH_USE_JWT = True
SIMPLE_AUTH_EMAIL_VERIFICATION = True
RESEND_API_KEY = 're_123456...'
LOGIN_REDIRECT_URL = '/'

Custom User Model Support

This package is designed to work seamlessly with custom user models.

  1. Define your model:

    # accounts/models.py
    from django.contrib.auth.models import AbstractUser
    from django.db import models
    
    class CustomUser(AbstractUser):
        phone_number = models.CharField(max_length=15, unique=True)
        avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)
    
  2. Update Settings:

    AUTH_USER_MODEL = 'accounts.CustomUser'
    SIMPLE_AUTH_USER_FIELDS = ['phone_number', 'avatar']
    
  3. Important: If you have file fields (like avatar), ensure your signup template form has enctype="multipart/form-data".

Troubleshooting

Common Issues

Problem: "No module named 'simple_auth'"
Solution: Install the package: pip install django-simple-auth

Problem: "CSRF token missing or incorrect"
Solution: API endpoints don't need CSRF. Use /api/auth/api/ endpoints, not /api/auth/ (HTML views).

Problem: "Passwords do not match"
Solution: Ensure password and confirm_password are identical in your request.

Problem: "Current password is incorrect"
Solution: Double-check you're entering your current password, not the new one.

Problem: "Password must be at least 8 characters long"
Solution: Use a password with 8 or more characters.

Getting Help

  1. Check the Quickstart Guide
  2. See Examples
  3. Run python manage.py simpleauth_check to verify your setup
  4. Open an issue on GitHub

License

MIT

API Documentation

If SIMPLE_AUTH_USE_JWT = True, the following endpoints are available under /auth/api/:

1. Signup

  • Endpoint: POST /auth/api/signup/
  • Payload:
    {
      "username": "jdoe",
      "email": "jdoe@example.com",
      "password": "securepassword",
      "confirm_password": "securepassword",
      "phone_number": "1234567890" // Any extra field defined in settings
    }
    
  • Response: 201 Created with access and refresh tokens.

2. Login

  • Endpoint: POST /auth/api/login/
  • Payload:
    {
      "username": "jdoe@example.com", // Or username/phone based on config
      "password": "securepassword"
    }
    
  • Response: 200 OK with access and refresh tokens.

3. Password Change

  • Endpoint: POST /auth/api/password-change/
  • Headers: Authorization: Bearer <access_token>
  • Payload:
    {
      "old_password": "oldpassword",
      "new_password": "newpassword"
    }
    

4. Password Reset Request

  • Endpoint: POST /auth/api/password-reset/
  • Payload: {"email": "jdoe@example.com"}

5. Password Reset Confirm

  • Endpoint: POST /auth/api/password-reset-confirm/
  • Payload:
    {
      "uidb64": "<uid from email>",
      "token": "<token from email>",
      "new_password": "newpassword"
    }
    

6. Resend Verification Email

  • Endpoint: POST /auth/api/resend-verification/
  • Payload: {"email": "jdoe@example.com"}

Template Customization

To customize the look and feel, create a directory named simple_auth inside your project's templates directory. You can override any of the following files:

  • simple_auth/base.html (Base layout)
  • simple_auth/login.html
  • simple_auth/signup.html
  • simple_auth/password_reset_form.html
  • simple_auth/password_reset_done.html
  • simple_auth/password_reset_confirm.html
  • simple_auth/password_reset_complete.html
  • simple_auth/password_change_form.html
  • simple_auth/password_change_done.html
  • simple_auth/verification_sent.html
  • simple_auth/verification_success.html

Example simple_auth/login.html override:

{% extends 'base.html' %}

{% block content %}
<div class="my-custom-login-class">
    <h2>Login to My App</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="btn btn-primary">Login</button>
    </form>
</div>
{% endblock %}

Email Verification

To enable email verification:

  1. Set SIMPLE_AUTH_EMAIL_VERIFICATION = True.
  2. Set RESEND_API_KEY = 're_...'.
  3. When a user signs up, is_active will be set to False.
  4. An email will be sent with a verification link.
  5. Clicking the link will activate the user and redirect to the success page.

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_simple_auth_pkg-1.0.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

django_simple_auth_pkg-1.0.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file django_simple_auth_pkg-1.0.0.tar.gz.

File metadata

  • Download URL: django_simple_auth_pkg-1.0.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for django_simple_auth_pkg-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4820c4c25a61c24e688b9d10cc8eacb54e763ddff9816276041d0f2b7f422f95
MD5 b936df7127caa313516bfef983dc62ed
BLAKE2b-256 1ce9b12628c903bb58ea675deac0a2258304ec7f1e47f8816b0603dde648dc3e

See more details on using hashes here.

File details

Details for the file django_simple_auth_pkg-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_simple_auth_pkg-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81b36485afa330cf5db709ebd5dbe4d7f8b66105c1cd9b839276100419f40097
MD5 9d5c6517037f864b6a7db1338f49fc0f
BLAKE2b-256 2076537d5a1ec5b0373e2f764f753299cf8f0124005a62b978e4eafc363178f4

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