Full Stack React & Django [5] - Django Token Authentication
Why Stack Overflow recommends it
Stack Overflow users link it as the reference tutorial for wiring Knox token authentication into a Django REST backend that a React frontend talks to, and cite it when debugging that setup.
Recommended by 2 people across 2 different months, 2019 to 2021.
What it is
Part 5 of Traversy Media's Full Stack React & Django series. Brad Traversy builds the backend authentication layer using Django REST Framework and Knox token auth, creating an accounts app with serializers, API views and endpoints for register, login and user lookup.
What people said
Recommends Akash Shendage · 2021-07 · Stack Overflow · on “Django Rest Framework - Authentication credentials were not provided”
if anyone come here from Full Stack React & Django [5] - Django Token Authentication - Traversy Media [this video] So you need to something like this accounts/api.py from rest_framework import generics, permissions from rest_framework.response import Response from knox.models import AuthToken from .serializers import LoginSerializer, RegisterSerializer, UserSerializer from knox.auth import TokenAuthentication # Register Api class RegisterAPI(generics.GenericAPIView): serializer_class = RegisterSerializer def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.save() return Response({ "user": UserSeri
Recommends mendespedro · 2021-09 · Stack Overflow · on “Custom permissions for different user types django rest framework”
You need to check if the user has permission every time he is making an action, so when the React app calls your Django API, it will provide an authentication token right? That tokens corresponds to a unique user, so you can just do an if statement: if request.user.is_admin: do_everything() elif request.user.is_moderator: do_other_stuff() While in the react app you would need the information if the logged in user is a moderator, admin or a normal user, so you can display the pages accordingly. To get that info, you may want to implement a '/me' endopoint that returns info about the logged in user, containg his status. If you have no idea what Im talking about, I strongly recommend you to tak
Cites Greg Holst · 2019-07 · Stack Overflow · on “How to force logout when Knox created token has expired?”
I developed my Django based webapp with token authentication by following this tutorial of Brad Traversy ( [this video] ) using Knox for authentication and React/Redux for the frontend. Login/logout works fine (here is Brad's code: [LINK] --> logout using a POST request), besides one issue: When the user stays away from the computer for a long time, the token expires in the meanwhile. So when the user returns they are still in the logged in zone of the website, but as soon as they open a React component with data loading from the DB a 401 error is thrown in the console ("Failed to load resource: the server responded with a status of 401 (Unauthorized)"). Then the user has to go on "logout" and lo
Cites Nick Weimer · 2020-07 · Stack Overflow · on “Django DRF OneToOneField for muliple user types”
Hey got my code below to work for serializing registration with multiple user types. I followed this: [LINK] here is my models.py: from django.contrib.auth.models import AbstractUser from django.db import models class User(AbstractUser): is_individual = models.BooleanField(default=False) is_company = models.BooleanField(default=False) class Company(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) company_name = models.CharField(max_length=100) email_address = models.EmailField(max_length=254, blank=True, null=True) class Individual(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) email_address = models.Em
Cites Shamar Yarde · 2021-04 · Stack Overflow · on “Share auth between django and react”
Once the user logs in, get the token from the backend using the following: axios.post(` [LINK] { headers: { 'Content-Type': 'application/json', data: { username, password }) .then(response => { console.log(`token: ${response.token}`); }) Port number could be 8000 and url pattern could be /api/auth/login for example. This answer mostly comes from the last reference. References Youtube. Full Stack React & Django [1] - Basic REST API. [LINK] . (Accessed 23 April, 2021). Youtube. Full Stack React & Django [2] - Implementing React. [LINK] . (Accessed 23 April, 2021). Youtube. Full Stack React & Django [3] - Redux & HTTP. [LINK] . (Accessed 23 April, 2021). Youtube. Full Stack React & Django [4] -