HI,
I have created a simple login API through django rest framework. Below is the 
code snippet:
from django.contrib.auth import login
from rest_framework.generics import ListCreateAPIView
from rest_framework import status
from rest_framework.response import Response
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.permissions import IsAuthenticated, IsAdminUser, AllowAny
from knox.views import LoginView as KLView
from knox.models import AuthToken
from .serializers import RegistrationSerializers
from .models import NewEmployeeProfile
class loginAPIView(KLView):
    permission_classes = [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(loginAPIView, self).post(request, format=None) URLS.pyfrom 
django.urls import pathfrom knox import views as knox_viewsfrom .views import 
UserRegisterView, loginAPIView

urlpatterns = [    path('register/', UserRegisterView.as_view(), 
name='register'),    path('login/', loginAPIView.as_view(), name='login'),    ]

Now when I do a POST request in postman with the email and the password. It 
throws error:
{    "detail": "Invalid token."}
Note:: Since I am using knox to generate token. So, when I do a new 
registration I get token as well. See sample below:
{    "status": "OK",    "message": {        "email": "[email protected]",      
  "first_name": "est",        "last_name": "Sah",        "employee_code": 
"6124368",        "contact": "7500078619",        "dob": null    },    "token": 
"db058f23ecc70f4fa3de4ac69a04dc48bb7579a63aea1ad3d038ce59b1511890"
I tried both, password and token to authenticate, but I am getting the same 
error. In the cmd prompt where the dev server is running, I am seeing below 
message
========================[08/Apr/2021 15:26:33] "POST /apii/login/ HTTP/1.1" 403 
27Forbidden: /apii/login/========================In the postman raw body, I am 
inserting below json data
{    "email": "[email protected]",    "password": 
"db058f23ecc70f4fa3de4ac69a04dc48bb7579a63aea1ad3d038ce59b1511890"}

Below is the settings.py content for DRF
REST_FRAMEWORK = {    'DEFAULT_AUTHENTICATION_CLASSES': [        
#'rest_framework.authentication.TokenAuthentication',        
'rest_framework.authentication.SessionAuthentication',        
'knox.auth.TokenAuthentication',    ],}


httpie output
python.exe -m httpie 127.0.0.1:8000/apii/login/ 'Authorization: Token 
db058f23ecc70f4fa3de4ac69a04dc48bb7579a63aea1ad3d038ce59b1511890'
HTTP/1.1 405 Method Not AllowedAllow: POST, OPTIONSContent-Length: 
40Content-Type: application/jsonDate: Thu, 08 Apr 2021 11:52:31 
GMTReferrer-Policy: same-originServer: WSGIServer/0.2 CPython/3.6.8Vary: 
Accept, CookieX-Content-Type-Options: nosniffX-Frame-Options: DENY
{    "detail": "Method \"GET\" not allowed."}

urlpatterns = [ path('register/', UserRegisterView.as_view(), name='register'), 
path('login/', loginAPIView.as_view(), name='login'), ]Not sure if I am doing 
it the right way, please suggest. Thank you....

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2028966691.640551.1617886057684%40mail.yahoo.com.

Reply via email to