简体   繁体   English

注销后后端未删除 dj-rest-auth 令牌

[英]dj-rest-auth token not deleted in backend after logout

I started using the django dj-rest-auth package with react and i ran across the following issue:我开始使用带有 react 的 django dj-rest-auth 包,我遇到了以下问题:

When i logout it will clear the key inside the local storage but not inside the Django backend.当我注销时,它将清除本地存储内的密钥,但不会清除 Django 后端内的密钥。

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.当我转到 API 端点页面并单击“发送发布请求”按钮时,它会执行此操作,但当我使用前端注销时它不会执行此操作。

DJ-休息-身份验证

This is the code i'm using to accomplish this:这是我用来完成此操作的代码:

navbar.js导航栏.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 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应用程序.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 : <Navigate replace to="/login" />
}

export default function App() {
  return (
    <Router>
      <AuthContextProvider>
        <div>
          <Navbar />          

          {/* A <Routes> looks through its children <Route>s and
              renders the first one that matches the current URL. */}
          <div className="max-w-4xl mx-auto py-5 px-4">
            <Routes>
              <Route path="/settings" element={<PrivateRoute><Settings /></PrivateRoute>} exact />
              <Route path="/login" element={<Login />} exact />
              <Route path="/signup" element={<Signup />} exact />
              <Route path="/reset" element={<Reset />} exact />
              <Route path="/password-reset/confirm/:uid/:token" element={<ResetConfirm />} exact />
              <Route path="/accounts/confirm-email/:key" element={<ConfirmEmail />} exact />
            </Routes>
          </div>
        </div>
      </AuthContextProvider>
    </Router>
  );
}

Apparently this is not possible so i started using JWT Tokens instead which are part of dj-rest-auth aswell:显然这是不可能的,所以我开始使用 JWT 代币,它们也是 dj-rest-auth 的一部分:

REST_USE_JWT = True

More details on the JWT configuration can be found here: https://django-rest-framework-simplejwt.readthedocs.io/en/latest/index.html可以在此处找到有关 JWT 配置的更多详细信息: https ://django-rest-framework-simplejwt.readthedocs.io/en/latest/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM