From 3888663434f9f44221feda2a9e77445aec582355 Mon Sep 17 00:00:00 2001 From: cwkang Date: Mon, 9 Jul 2018 21:34:11 +0800 Subject: [PATCH] Added a new section in auth docs that provides an example implementation for devs that aim to use Knox Token Authentication as their only authentication method. --- docs/auth.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/auth.md b/docs/auth.md index 3b66a901..5872373a 100644 --- a/docs/auth.md +++ b/docs/auth.md @@ -72,3 +72,37 @@ urlpatterns = [ ``` You can use any number of authentication classes if you want to be able to authenticate using different methods (eg.: Basic and JSON) in the same view. Just be sure not to set TokenAuthentication as your only authentication class on the login view. + +If you decide to use Token Authentication as your only authentication class, you can overwrite knox's login view as such: + +```python + +views.py: + +from django.contrib.auth import login + +from rest_framework import permissions +from rest_framework.authtoken.serializers import AuthTokenSerializer +from knox.views import LoginView as KnoxLoginView + +class LoginView(KnoxLoginView): + permission_classes = (permissions.AllowAny,) + + def post(self, request, format=None): + serializer = AuthTokenSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + user = serializer.validated_data['user'] + login(request, user) + return super(LoginView, self).post(request, format=None) + +urls.py: + +from knox import views as knox_views +from yourapp.api.views import LoginView + +urlpatterns = [ + url(r'login/', LoginView.as_view(), name='knox_login'), + url(r'logout/', knox_views.LogoutView.as_view(), name='knox_logout'), + url(r'logoutall/', knox_views.LogoutAllView.as_view(), name='knox_logoutall'), +] +``` \ No newline at end of file