浏览代码

Implemented standard user authentication using DRF which returns token

master
kj1352 3 年前
父节点
当前提交
ac99757bad
共有 3 个文件被更改,包括 27 次插入18 次删除
  1. +5
    -7
      workx_backend/settings.py
  2. +4
    -10
      workx_backend/urls.py
  3. +18
    -1
      workx_backend/views.py

+ 5
- 7
workx_backend/settings.py 查看文件

@@ -15,7 +15,6 @@ from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent



# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/


@@ -27,7 +26,6 @@ DEBUG = True


ALLOWED_HOSTS = [] ALLOWED_HOSTS = []



# Application definition # Application definition


INSTALLED_APPS = [ INSTALLED_APPS = [
@@ -37,7 +35,8 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework'
'rest_framework',
'rest_framework.authtoken',
] ]


MIDDLEWARE = [ MIDDLEWARE = [
@@ -57,6 +56,9 @@ REST_FRAMEWORK = {
# or allow read-only access for unauthenticated users. # or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [ 'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication'
] ]
} }


@@ -78,7 +80,6 @@ TEMPLATES = [


WSGI_APPLICATION = 'workx_backend.wsgi.application' WSGI_APPLICATION = 'workx_backend.wsgi.application'



# Database # Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases # https://docs.djangoproject.com/en/4.0/ref/settings/#databases


@@ -89,7 +90,6 @@ DATABASES = {
} }
} }



# Password validation # Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators


@@ -108,7 +108,6 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]



# Internationalization # Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/ # https://docs.djangoproject.com/en/4.0/topics/i18n/


@@ -120,7 +119,6 @@ USE_I18N = True


USE_TZ = True USE_TZ = True



# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/ # https://docs.djangoproject.com/en/4.0/howto/static-files/




+ 4
- 10
workx_backend/urls.py 查看文件

@@ -1,21 +1,15 @@
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include

# Serializers define the API representation.
from rest_framework import routers 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 = routers.DefaultRouter()
router.register(r'users', UserViewSet) router.register(r'users', UserViewSet)


# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.

urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('api/', include(router.urls)), path('api/', include(router.urls)),
path('api-auth/', include('rest_framework.urls'))
path('authenticate/', CustomAuthToken.as_view())
] ]


+ 18
- 1
workx_backend/views.py 查看文件

@@ -1,9 +1,26 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import viewsets 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 workx_backend.serializers import UserSerializer
from rest_framework.response import Response




class UserViewSet(viewsets.ModelViewSet): class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all() queryset = User.objects.all()
serializer_class = UserSerializer 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
})