AxiosError: Request failed with status code 403 - login and token

auth.js

export async function loginUser(data) {
    const response = await axios.post(
        'http://10.0.2.2:8000/api/rest-auth/login/',
        {
            username: data.username,
            password1: data.password,
        },
        {
            headers: {
                "Content-Type": "application/json",
            }
        }
    );
    console.log(response.data)

LoginScreen.js

import { useState } from 'react';
import LoadingOverlay from '../../components/ui-components/LoadingOverlay';
import AuthContent from '../../components/auth-components/AuthContent';
import { loginUser } from '../../util/auth';

function LoginScreen() {
  const [isAuthenticating, setIsAuthenticating] = useState(false);
  
  async function loginHandler(username, password1) {
    setIsAuthenticating(true);
    await loginUser(username, password1);
    setIsAuthenticating(false);
  }

  if (isAuthenticating) {
    return <LoadingOverlay message="Logging you in..."/>
  }

  return <AuthContent isLogin onAuthenticate={loginHandler}/>;
}

export default LoginScreen;

setting.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',       
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',       
    )
}

I've tried logging into both the REST API and Postman, and everything works fine - it returns the token to me once the right credentials are issued. But when I try to send the request via the login form from react native to django via axios, it gives me the error 403 forbidden by axios. On the contrary, the registration makes me carry it out both via REST API and via the registration form (React Native) and returns the token.

Back to Top