title: Django:authorization fails when getting data with the token
body: <p>After registering user, when I login,  I get access token, and after that I want to get the user details back, but I am unable to to so. In <code>Login.js</code>  authentication works and I get back  access and refresh token, then I want to send another get request using that token,in order to get user details, but it gives unauthorized error.</p>
  <blockquote>
  <p>backend</p>
  </blockquote>
  <p>views.py</p>
  <pre><code>import json
  from multiprocessing import context
  from django.http import HttpResponse, JsonResponse
  from django.shortcuts import render
  from rest_framework.decorators import api_view,permission_classes,authentication_classes
  from oyren.serializers import CustomUserSerializer
  from oyren.models import NewUser
  from rest_framework import permissions
  
  
  
  @api_view(['GET'])
  @permission_classes([permissions.IsAuthenticated])  #authorization fails
  def get_user_details(request):
      data={
          'user':request.user.name ,
          'email':request.user.email
      }
      return  JsonResponse(data)
  
  
  @api_view(['POST']) #register view
  def register(request):
     print(request.data)
     serializer=CustomUserSerializer(data=request.data);
     if serializer.is_valid():
         instance=serializer.save();
         instance.set_password(request.data['password'])
         instance.save();
         print(&quot;serializer is valid&quot;)
         return HttpResponse(&quot;serializer is valid&quot;)
     else:
          print('serializer is not valid')
          print(serializer.errors)
          return HttpResponse(&quot;serializeris not valid&quot;) 
  
  # Create your views here.
  
  </code></pre>
  <p>settings.py</p>
  <pre><code>INSTALLED_APPS = [
      'oyren',
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'corsheaders',
      'rest_framework',
      'rest_framework.authtoken',
  ]
  
  REST_FRAMEWORK={
      'DEFAULT_PERMISSION_CLASSES':[
          'rest_framework.permissions.AllowAny'
      ],
      'DEFAULT_AUTHENTICATION_CLASSES':[
          'rest_framework_simplejwt.authentication.JWTAuthentication'
      ]
  }
  
  SIMPLE_JWT = {
      'ACCESS_TOKEN_LIFETIME': timedelta(minutes=1),
      'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
      'ROTATE_REFRESH_TOKENS': False,
      'BLACKLIST_AFTER_ROTATION': False,
      'UPDATE_LAST_LOGIN': False,
  
      'ALGORITHM': 'HS256',
      'SIGNING_KEY': SECRET_KEY,
      'VERIFYING_KEY': None,
      'AUDIENCE': None,
      'ISSUER': None,
      'JWK_URL': None,
      'LEEWAY': 0,
  
      'AUTH_HEADER_TYPES': ('Bearer',),
      'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION',
      'USER_ID_FIELD': 'id',
      'USER_ID_CLAIM': 'user_id',
      'USER_AUTHENTICATION_RULE': 'rest_framework_simplejwt.authentication.default_user_authentication_rule',
  
      'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
      'TOKEN_TYPE_CLAIM': 'token_type',
      'TOKEN_USER_CLASS': 'rest_framework_simplejwt.models.TokenUser',
  
      'JTI_CLAIM': 'jti',
  
      'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
      'SLIDING_TOKEN_LIFETIME': timedelta(minutes=1),
      'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=1),
  }
  
  </code></pre>
  <p>urls.py</p>
  <pre><code>urlpatterns = [
    path('register/',register),
    path('get_access_token/',TokenObtainPairView.as_view()),
    path('get_refresh_token/',TokenRefreshView.as_view()),
    path('get_user_details/',get_user_details)
  
  ]
  </code></pre>
  <blockquote>
  <p>frontend</p>
  </blockquote>
  <p>Login.js</p>
  <pre><code>import img from '../images/x7.png'
  import register from '../images/register.png'
  import logo from '../images/logo.png'
  import {Link,Route,Routes }from 'react-router-dom'
  import Style from '../styles/Login.module.css'
  import BASE_URL from './axios'
  import { useNavigate } from 'react-router'
  import axios  from 'axios'
  const Login = ()=&gt;{
      const history = useNavigate();
      const handleSubmit=(e)=&gt;{
          e.preventDefault();
          let  data={
              'email':e.target[0].value,
              'password':e.target[1].value
          }
          axios.post(`${BASE_URL}/get_access_token/`,data) 
          .then((res)=&gt;{
              localStorage.setItem('access_token',res.data.access)
              localStorage.setItem('refresh_token',res.data.refresh) //access and refresh token successfully obtained and stored 
              history('/teacher_home_page')
              return res
  
          })
          .then((res)=&gt;{
              let access_token=localStorage.getItem('access_token')  
              console.log(access_token)
              let config={
                  headers:{
                      Authorization: &quot;Bearer&quot;+ &quot; &quot; +  access_token  
                    }
                   }
              axios.get(`${BASE_URL}/get_user_details/`,config)//trying to use that token to send another request,requesting user details
              .then((res)=&gt;{
                  console.log(res)
              })
          })
          .catch((res)=&gt;{
              alert(&quot;USER CREDENTIALS DID NOT MATCH&quot;)
          })
      }
      return (
          &lt;&gt;
        &lt;div className={Style.container}&gt;
         &lt;div className={Style.logoContainer}&gt;
         &lt;img className ={Style.logo} src={logo}&gt;&lt;/img&gt;
         &lt;img className={Style.background} src={register}&gt;&lt;/img&gt;
         &lt;/div&gt;
  &lt;div className={Style.inputContainer}&gt;
      &lt;p  className={Style.welcome}&gt;
          WELCOME TO THE  &lt;br /&gt;
          LEARNING  &lt;br /&gt;
          PLATFORM!
      &lt;/p&gt;
      &lt;form onSubmit={handleSubmit} className={Style.form}&gt;
          &lt;input  placeholder=&quot;Email&quot;/&gt;
          &lt;input  placeholder=&quot;Password&quot;/&gt;
          &lt;button type='submit'&gt;Log In&lt;/button&gt;
          &lt;div className={Style.links}&gt;  
           &lt;Link to={'/register_as_student'}&gt;  &lt;a&gt;Register as student&lt;/a&gt;&lt;/Link&gt;
           &lt;Link to={'/register_as_teacher'}&gt;  &lt;a&gt;Register as teachet&lt;/a&gt;&lt;/Link&gt;
          &lt;/div&gt; 
      &lt;/form&gt;
  &lt;/div&gt;
        &lt;/div&gt;
          &lt;/&gt;
      )
  
  }
  
  export  default Login
  </code></pre>