Skip to content

blueswen/django-custom-user

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Custom User with DRF and Simple JWT

According to Django Document, it's highly recommended to set up a custom user model when starting a new project. This project is a sample to demo how to create custom user model, and using Django REST framework and SimpleJWT for API interface and token based authentication.

Check more detail in my blog post Django Custom User with DRF and Simple JWT.

Dependencies

python >= 3.6

  1. Django==3.1.4
  2. djangorestframework==3.12.2
  3. djangorestframework-simplejwt==4.6.0

Usage

  1. Create python virtualenv

    $ python -m venv .venv
    $ source .venv/bin/activate
  2. Install requirements

    (.venv) $ pip install -r requirements.txt
  3. Migrate DB

    (.venv) $ python manage.py makemigrations
    (.venv) $ python manage.py migrate
  4. Create superuser

    (.venv) $ python manage.py createsuperuser
  5. Run Server

    (.venv) $ python manage.py runserver
  6. Get token from API

    $ curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"username": "admin", "password": "admin"}' \
    http://localhost:8000/api/token/
    
    {"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYwNzQ0MzQ3NiwianRpIjoiYzQzYjM2Zjg2ODA0NDU1MzliYzUwNTlmN2YzN2NkMTEiLCJ1c2VyX2lkIjoxfQ.ZC0fAj7HR99v_po4BI-uVVeS9c7ZoN4B35_pYzosE_o","access":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjA3MzU3Mzc2LCJqdGkiOiIyYjY5NzkxMmZhNjE0NzY3YmJkNDA2NjExMzE1YzkxMCIsInVzZXJfaWQiOjF9.92m-V9vjRxUWGLlcJRFBdLqSHp0UII3SLPTt_yPynqY"}
  7. Get user list with token

    $ curl -H 'Accept: application/json; indent=4' -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjA3MzU3Mzc2LCJqdGkiOiIyYjY5NzkxMmZhNjE0NzY3YmJkNDA2NjExMzE1YzkxMCIsInVzZXJfaWQiOjF9.92m-V9vjRxUWGLlcJRFBdLqSHp0UII3SLPTt_yPynqY" http://127.0.0.1:8000/api/account/user/
    [
        {
            "id": 1,
            "password": "pbkdf2_sha256$216000$fHERFJ7toIcI$3QOXbA4or+srGXn+60aW+z4rslvJkQcW2wS0oWWzYHI=",
            "last_login": "2020-12-07T16:02:06.847562Z",
            "is_superuser": true,
            "username": "admin",
            "first_name": "",
            "last_name": "",
            "email": "admin@sample.com",
            "is_staff": true,
            "is_active": true,
            "date_joined": "2020-12-07T13:54:21.479125Z",
            "birthday": null,
            "groups": [],
            "user_permissions": []
        }
    ]

Reference

  1. Django Custom User Model
  2. Django REST framework
  3. SimpleJWT Document

About

Django Custom User with DRF and Simple JWT

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages