Skip to main content
https://travis-ci.org/anthon-alindada/django_custom_auth_user.svg?branch=master https://codecov.io/gh/anthon-alindada/django_custom_auth_user/branch/master/graph/badge.svg

Django custom user model and abstract user model basic token authentication.

Documentation

The full documentation is at https://django_custom_auth_user.readthedocs.io.

Quickstart

Install Django custom user:

pip install django_custom_auth_user

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'custom_auth_user',
    ...
)

Set your AUTH_USER_MODEL setting to use custom_auth_user.User:

AUTH_USER_MODEL = 'custom_auth_user.User'

Create the database tables:

python manage.py migrate

Usage

Custom user

Use get_user_model() to get user. For example:

from django.contrib.auth import get_user_model

user = get_user_model().objects.get(email="user@cloud.com")

Use AUTH_USER_MODEL for model relations. For example:

from django.conf import settings
from django.db import models

class Post(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL)

Or you can use custom_auth_user.models.AbstractUser to extend custom user. For example:

from custom_auth_user.models import AbstractUser

class CustomUser(AbstractUser):
    """
    User model extends AbstractUser
    """
    pass

Then change the AUTH_USER_MODEL in settings to use new custom user:

AUTH_USER_MODEL = 'app.CustomUser'

User registration service

Use RegistrationService to register new user:

from custom_auth_user.user.registration import RegistrationService
from custom_auth_user.user.exceptions import InvalidInput

# Initialize registration service
registration_service = RegistrationService(
    email=request.POST.get('email', ''),
    username=request.POST.get('username', ''),
    first_name=request.POST.get('first_name', ''),
    last_name=request.POST.get('last_name', ''),
    password=request.POST.get('password', ''))

# Catch errors here
try:
    user = registration_service.run()
except InvalidInput:
    errors = registration_service.get_registration_form_errors()

Generate auth token

Use AuthenticateUserService to generate new auth token:

from custom_auth_user.auth_token.authenticate_user import AuthenticateUserService
from custom_auth_user.auth_token.exceptions import AuthenticationFailed

authentication_service = AuthenticateUserService(
    email_or_username=request.POST.get('email_or_username', ''),
    password=request.POST.get('password', ''))

try:
    token = authentication_service.run()
except AuthenticationFailed:
    # Authentication failed
    pass

Token authentication

Use token_required decorator to authenticate header token. Get authenticated user at request.user.

Authorization header must have token at the begining. Example token 5KxXkJYwWBsN9Zne87ncoQYYuggDBdYY.

Return json response if authentication failed:

from custom_auth_user.auth_token.decorators import token_required

@token_required
def my_view(request):
    # get authenticated user at request.user
    pass

Or you can use AuthenticateTokenService to authenticate token:

from custom_auth_user.auth_token.authenticate_token import AuthenticateTokenService
from custom_auth_user.auth_token.exceptions import AuthenticationFailed

authentication_service = AuthenticateTokenService(
    auth_token=request.POST.get('token', ''))

try:
    user = authentication_service.run()
except AuthenticationFailed:
    # Authentication failed
    pass

Delete Token

Use DeleteTokenService to delete token. You can use this when user logs out:

from custom_auth_user.auth_token.delete_token import DeleteTokenService
from custom_auth_user.auth_token.exceptions import TokenNotFound

delete_token_service = DeleteTokenService(
    token=request.POST.get('token', ''))

try:
    delete_token_service.run()
except TokenNotFound:
    # Token not found
    pass

Extra features

User QuerySets

# Get all users
users = User.objects.get_all()

# Get find user by id
user = User.objects.find_by_id(id=1)

# Get find usermame by id
user = User.objects.find_by_username(username='user')

# Get find email by id
user = User.objects.find_by_email(email='user@cloud.com')

# Filter active users
users = User.objects.filter_by_active()

# Filter inactive users
users = User.objects.filter_by_inactive()

# Filter enabled users
users = User.objects.filter_by_enabled()

# Filter disabled users
users = User.objects.filter_by_disabled()

AuthToken QuerySets

# Get all auth token
token = AuthToken.objects.get_all()

# Find by id
token = AuthToken.objects.find_by_id(id=1)

# Find by token
token = AuthToken.objects.find_by_token(token='token')

# Filter by active or unexpired tokens
token = AuthToken.objects.filter_by_active()

# Filter by expired token
token = AuthToken.objects.filter_by_expired()

Download files

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

Source Distribution

django-custom-auth-user-0.1.1.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

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

django_custom_auth_user-0.1.1-py2.py3-none-any.whl (21.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-custom-auth-user-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-custom-auth-user-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9a5ab5b66dd04d46e343f9e507581d3abeff0fb60c3758dcc7d1db283e98796d
MD5 647a88e9616770d3fff58d8062125ce6
BLAKE2b-256 33acb5b633d30925fd48e9e55b3345a21da200e82ef0b8e0d65d21c14b27b876

See more details on using hashes here.

File details

Details for the file django_custom_auth_user-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_custom_auth_user-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 572ef9e0a190642a53b24ca8b4fe4bb251b7674ca00403ef7c89d86fde75899d
MD5 2f62a4d0250ca73d429c4275a780e5d5
BLAKE2b-256 d463bcc01bbbcb928f8513c1ffb875361fc70dd852749abad27263486a97ce67

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.2

2 files

This release

0.1.1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page