[PYTHON] Implementing authentication in Django REST Framework with djoser

What is djoser

djoser is a library that supports basic user authentication and registration on the Django REST Framework. It can also be used for custom models, and is designed for an architecture that fits better with a Single Page Application (SPA) rather than reusing Django's code.

This time I will write about the implementation of the simplest authentication function of djoser. Please note that this authentication should not be actually used for security reasons, and there are stronger security settings such as the JWT authentication below. I will introduce it as a simple authentication to the last.

JWT authentication settings are explained in here.

Source code here

All of the following can be used as endpoints after installation.

/users/ /users/me/ /users/confirm/ /users/resend_activation/ /users/set_password/ /users/reset_password/ /users/reset_password_confirm/ /users/set_username/ /users/reset_username/ /users/reset_username_confirm/ /token/login/ (Token Based Authentication) /token/logout/ (Token Based Authentication) /jwt/create/ (JSON Web Token Authentication) /jwt/refresh/ (JSON Web Token Authentication) /jwt/verify/ (JSON Web Token Authentication) Getting started

How to use

First of all, from the installation.

$ pip install -U djoser

First, make a project,

$ django-admin startproject simple_djoser_authentication

Go within the project.

$ cd simple_djoser_authentication

We'll set up Django.

setings.py




   .........

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework', # add
    'djoser' # add

]

urls.py


from django.contrib import admin
from django.urls import path,include #add

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/auth/',include('djoser.urls')), #add
]

Only this.

After this, migrate, create an Admin user and launch it locally.

$ python manage.py migrations
$ python manage.py createsuperuser
Username: Admin
Email address: [email protected]
Password:*********** 
$ python manage.py runserver

And http://localhost:8000/api/auth/ To access.

Then スクリーンショット 2020-04-19 21.14.38.png

The above screen will be displayed.

And then http://localhost:8000/api/auth/users/ When you access

スクリーンショット 2020-04-19 22.07.58.png

The above list of user information is displayed.

This completes the introduction of the basic functions around authentication.

Recommended Posts

Implementing authentication in Django REST Framework with djoser
Implementation of custom user model authentication in Django REST Framework with djoser
Django REST framework with Vue.js
Login with django rest framework
More new user authentication methods with Django REST Framework
Create APIs around user authentication with Django REST Framework
Implement hierarchical URLs with drf-nested-routers in Django REST framework
[Django] Use MessagePack with Django REST framework
Create RESTful APIs with Django Rest Framework
CRUD GET with Nuxt & Django REST Framework ②
CRUD POST with Nuxt & Django REST Framework
CRUD GET with Nuxt & Django REST Framework ①
CRUD PUT, DELETE with Nuxt & Django REST Framework
Django REST framework basics
Implement JWT login functionality in Django REST framework
How to deal with garbled characters in json of Django REST Framework
Create a Todo app with Django REST Framework + Angular
Create a Todo app with the Django REST framework
When you want to filter with Django REST framework
List method for nested resources in Django REST framework
Django REST framework stumbling block
How to write custom validations in the Django REST Framework
Create an authentication feature with django-allauth and CustomUser in Django
reload in django shell with ipython
GraphQL API with graphene_django in Django
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
Create a REST API to operate dynamodb with the Django REST Framework
Understand the benefits of the Django Rest Framework
ng-admin + Django REST framework ready-to-create administration tool
Miscellaneous notes about the Django REST framework
Django REST Framework + Clean Architecture Design Consideration
How to automatically generate API document with Django REST framework & POST from document screen
Start Django in a virtual environment with Pipenv
A story about implementing a login screen with django
Django REST framework A little useful to know.
Configure a module with multiple files in Django
How to create a Rest Api in Django
Models in Django
Internationalization with django
CRUD with Django
Forms in Django
Learning notes for the migrations feature in the Django framework (2)
Python beginners tried implementing REST API in one day
Hit the Twitter API after Oauth authentication with Django
Try implementing associative memory with Hopfield network in Python
Implement APIs at explosive speed using Django REST Framework
Learning notes for the migrations feature in the Django framework (3)
Dynamically create tables in schema with Django, dynamically generate models
Learning notes for the migrations feature in the Django framework (1)
Creating an API that returns negative-positive inference results using BERT in the Django REST framework