Skip to content

退出登录流程

  • 向后端发送请求
  • 统一掐断飞行中的请求
  • 清理 axios 授权相关请求头
  • 重置所有 store,其中的 userStore 中有 useLocalStore 定义的 token,这也会同时清除 localstorage 中的所有 token 存储;
  • await nextTick()
  • 跳转到登录页

退出登录示例代码

ts
async function logout() {
  try {
    isLoggingOut.value = true;
    await http.post("/api/pub/loginOut");

    http.cancelAll?.();
    http.setAuthToken?.(null);

    resetAllStores();

    await nextTick();
  } catch (error) {
    console.error("[auth] logout error:", error);
  } finally {
    if (window.location.pathname !== LOGIN_ROUTE.path) {
      window.location.replace(LOGIN_ROUTE.path);
    }
    isLoggingOut.value = false;
  }
}

本站总访问