iMerica / dj-rest-auth

Authentication for Django Rest Framework
https://dj-rest-auth.readthedocs.io/en/latest/index.html
MIT License
1.67k stars 311 forks source link

Logout request not deleting Token inside the Django Backend #406

Closed DesignsToGrow closed 2 years ago

DesignsToGrow commented 2 years ago

I started using the django dj-rest-auth package with react and i ran across the following issue:

When i logout it will clear the key inside the local storage but not inside the Django backend.

When i go to the API endpoint page and click the "send post request" button it will do it, but it won't do it when i use my frontend to logout.

Screenshot 2022-05-09 112423

This is the code i'm using to accomplish this:

navbar.js `import { useContext } from "react"; import { Link, useNavigate } from "react-router-dom"; import axios from "axios" import { AuthContext } from "../contexts/AuthContext"; import { API } from "../api"

export function Navbar() { const { user, logout } = useContext(AuthContext) const navigate = useNavigate()

function handleSubmit() {
  axios.post(API.auth.logout)
    .then(res => {
      logout()
      navigate('/login')
    })
}`

api.js const baseURL = "http://127.0.0.1:8000" const apiURL =${baseURL}/api`

export const API = { auth: { login: ${baseURL}/dj-rest-auth/login/, logout: ${baseURL}/dj-rest-auth/logout/, passwordReset: ${baseURL}/dj-rest-auth/password/reset/, passwordResetConfirm: ${baseURL}/dj-rest-auth/password/reset/confirm/, signup: ${baseURL}/dj-rest-auth/registration/, verifyEmail: ${baseURL}/dj-rest-auth/registration/verify-email/ } } App.js import React, { useContext } from "react"; import { BrowserRouter as Router, Routes, Route, Navigate } from "react-router-dom"; import { AuthContext, AuthContextProvider } from './contexts/AuthContext'

import { Login } from './components/Login' import { Reset } from './components/Reset' import { Navbar } from "./components/Navbar"; import { Signup } from "./components/Signup" import { ConfirmEmail } from "./components/ConfirmEmail"; import { ResetConfirm } from './components/ResetConfirm'

function PrivateRoute({ children }) { const { user } = useContext(AuthContext) return user ? children : }

export default function App() { return (

{/* A looks through its children s and renders the first one that matches the current URL. */}
} exact /> } exact /> } exact /> } exact /> } exact /> } exact />

); }`

quroom commented 1 year ago

Did you fix it? I faced same problem.

DesignsToGrow commented 1 year ago

Yeah by using JWT_AUTH with dj-rest-auth. Which is more secure anyway.

quroom commented 1 year ago

Thanks for answering. :)