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/
Method | Request | Response |
---|---|---|
POST |
|
|
* http://localhost:8000/users/activation/
Method | Request | Response |
---|---|---|
POST |
|
|
For more hit urls go though -- https://djoser.readthedocs.io/en/latest/base_endpoints.html
Comments
Post a Comment