指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

Nuxt3下使用@sidebase_nuxt-auth管理用户登录信息操作:

  auth: {
    baseURL: "/api/auth",
    // https://sidebase.io/nuxt-auth/configuration/nuxt-config -> local provider
    provider: {
      type: "refresh", //这里把 local 改为 refresh 即可有 无状态刷新 token 的功能
      endpoints: {
        signIn: { path: "/login", method: "post" },
        getSession: { path: "/user", method: "get" },
        // signOut: { path: "/logout", method: "get" },
        refresh: { path: "/refresh", method: "post" }, // 这个需要打开,如果使用 local 的话,这个可以注释掉, 对应的下面的也要注释掉
      },
      pages: {
        login: "/auth/login",
      },
      token: {
        signInResponseTokenPointer: "/token/accessToken",
        sameSiteAttribute: "lax",
      },
      // refresh 下这个也需要打开
      refreshToken: {
        signInResponseRefreshTokenPointer: "/token/refreshToken",
      },
      // 这个就是 data 数据, 可以通过 getSession() 重新获取最新的用户数据
      sessionDataType: {
        id: "string",
        email: "string",
        first_name: "string",
        last_name: "string",
      },
    },
  },
  
server/api/refresh.post.ts文件:

  export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig();

  const body = await readBody(event);

  const unauthenticatedResponse = {
    token: {
      accessToken: "",
      refreshToken: "",
    },
  };
  
  if (!body.refreshToken) {
    return unauthenticatedResponse;
  }

  try {
    const data = await $fetch<{ token: string; refresh_token: string }>(
      "/api/token/refresh",
      {
        method: "POST",
        headers: { "X-API-TOKEN": config.duoxiaozhan.apiToken },
        body: {
          refresh_token: body.refreshToken,
        },
        baseURL: config.duoxiaozhan.baseUrl,
      }
    );
    
    return {
      token: {
        accessToken: data.token,
        refreshToken: data.refresh_token,
      },
    };
  } catch (error: any) {
    return unauthenticatedResponse;
  }
});

  
参考: https://sidebase.io/nuxt-auth/getting-started/quick-start