Use Djoser In Api's For Authentication In Django

Hint --- Please make sure you setup EmailBackend already setup another wise this is not work 

1. Install some library

pip install -U djoser 

pip install -U djangorestframework_simplejwt

pip install -U social-auth-app-django


2.  Add This in Your INSTALLED_APPS in setting.py file

    'rest_framework',

    'rest_framework.authtoken',

    'djoser'

 


3.  Add url in urls.py in project url file

urlpatterns = [
path('admin/', admin.site.urls),-- default
path('', include('editor.urls')), -- your apps url
path('', include('djoser.urls')), -- given urls in djoser
path('', include('djoser.urls.jwt')), -- for jwt
path('', include('djoser.urls.authtoken')), -- for login, logout


4. Add Rest frame Work in setting.py file

REST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',}

5. Add this also in your setting.py file

  SIMPLE_JWT = {

   'AUTH_HEADER_TYPES': ('JWT',),
}

6. Add Djoser setup in setting.py file 

DJOSER = {
'LOGIN_FIELD': 'email',
'USER_CREATE_PASSWORD_RETYPE': True,
'USERNAME_CHANGED_EMAIL_CONFIRMATION': True,
'PASSWORD_CHANGED_EMAIL_CONFIRMATION': True,
'SEND_CONFIRMATION_EMAIL': True,
'SEND_CONFIRMATION_EMAIL': True,
'SET_USERNAME_RETYPE': True,
'PASSWORD_RESET_CONFIRM_URL': 'password/reset/confirm/{uid}/{token}',
'USERNAME_RESET_CONFIRM_URL': 'email/reset/confirm/{uid}/{token}',
'ACTIVATION_URL': 'activate/{uid}/{token}',
'SEND_ACTIVATION_EMAIL': True,
'SERIALIZERS': {
'user_create': 'editor.serializers.UserCreateSerializer',
'user': 'editor.serializers.UserCreateSerializer',
'user_delete': 'djoser.serializers.UserDeleteSerializer',
}

}

7. Registration for user

url; 

* http://localhost:8000/users/

MethodRequestResponse
POST
  • {{ User.USERNAME_FIELD }}
  • {{ User.REQUIRED_FIELDS }}
  • password
  • re_password

HTTP_201_CREATED

  • {{ User.USERNAME_FIELD }}
  • {{ User._meta.pk.name }}
  • {{ User.REQUIRED_FIELDS }}

HTTP_400_BAD_REQUEST

  • {{ User.USERNAME_FIELD }}
  • {{ User.REQUIRED_FIELDS }}
  • password
  • re_password

* http://localhost:8000/users/activation/

MethodRequestResponse
POST
  • uid
  • token

HTTP_204_NO_CONTENT

HTTP_400_BAD_REQUEST

  • uid
  • token

HTTP_403_FORBIDDEN

  • detail



For more hit urls go though -- https://djoser.readthedocs.io/en/latest/base_endpoints.html



Comments

Popular posts from this blog

AttributeError: Got AttributeError when attempting to get a value for field `field_name` on serializer `ModelSerializer`.

DRF: Incorrect type. Expected pk value, received str - ManyToManyField in Django APIs