From ac99757bad0320e0c738554cc3406f32799fb3f6 Mon Sep 17 00:00:00 2001 From: kj1352 Date: Tue, 5 Jul 2022 18:57:30 +0530 Subject: [PATCH] Implemented standard user authentication using DRF which returns token --- workx_backend/settings.py | 12 +++++------- workx_backend/urls.py | 14 ++++---------- workx_backend/views.py | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/workx_backend/settings.py b/workx_backend/settings.py index 632875b..c7f746c 100644 --- a/workx_backend/settings.py +++ b/workx_backend/settings.py @@ -15,7 +15,6 @@ from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ @@ -27,7 +26,6 @@ DEBUG = True ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -37,7 +35,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'rest_framework' + 'rest_framework', + 'rest_framework.authtoken', ] MIDDLEWARE = [ @@ -57,6 +56,9 @@ REST_FRAMEWORK = { # or allow read-only access for unauthenticated users. 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' + ], + 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework.authentication.BasicAuthentication' ] } @@ -78,7 +80,6 @@ TEMPLATES = [ WSGI_APPLICATION = 'workx_backend.wsgi.application' - # Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases @@ -89,7 +90,6 @@ DATABASES = { } } - # Password validation # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators @@ -108,7 +108,6 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] - # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ @@ -120,7 +119,6 @@ USE_I18N = True USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.0/howto/static-files/ diff --git a/workx_backend/urls.py b/workx_backend/urls.py index ee2d3c0..894c93b 100644 --- a/workx_backend/urls.py +++ b/workx_backend/urls.py @@ -1,21 +1,15 @@ from django.contrib import admin from django.urls import path, include - -# Serializers define the API representation. from rest_framework import routers - - -# Routers provide an easy way of automatically determining the URL conf. -from workx_backend.views import UserViewSet +from workx_backend.views import UserViewSet, CustomAuthToken +from rest_framework.authtoken import views router = routers.DefaultRouter() router.register(r'users', UserViewSet) -# Wire up our API using automatic URL routing. -# Additionally, we include login URLs for the browsable API. - urlpatterns = [ path('admin/', admin.site.urls), path('api/', include(router.urls)), - path('api-auth/', include('rest_framework.urls')) + path('authenticate/', CustomAuthToken.as_view()) ] + diff --git a/workx_backend/views.py b/workx_backend/views.py index d88536e..df8f50c 100644 --- a/workx_backend/views.py +++ b/workx_backend/views.py @@ -1,9 +1,26 @@ from django.contrib.auth.models import User from rest_framework import viewsets - +from rest_framework.authtoken.models import Token +from rest_framework.authtoken.views import ObtainAuthToken from workx_backend.serializers import UserSerializer +from rest_framework.response import Response class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer + + +class CustomAuthToken(ObtainAuthToken): + + def post(self, request, *args, **kwargs): + serializer = self.serializer_class(data=request.data, + context={'request': request}) + serializer.is_valid(raise_exception=True) + user = serializer.validated_data['user'] + # token, created = Token.objects.get_or_create(user=user) + return Response({ + # 'token': token.key, + # 'user_id': user.pk, + 'email': user.email + })