Nuxt 3 @sidebase/nuxt-auth 模块 - 本地提供程序在登录后不会保留身份验证状态

问题描述 投票:0回答:1

我使用 @sidebase/[email protected] 创建了 nuxt 3 应用程序以进行身份验证。 它能够成功连接到后端 api(.NET) /login 和 /user-info,但登录后重定向时,仍然重定向回 /login 页面。

我尝试使用

await signIn({user_name: 'test', password: 'user'}, { redirect: false });
登录后阻止重定向,当我登录 useAuth() 时,状态为“已验证”并且令牌具有值。

nuxt.config.ts 中的身份验证配置

auth: {
        globalAppMiddleware: true,
        baseURL: process.env.API_BASE_URL,
        provider: {
            type: 'local',
            pages: { login: '/login' },
            endpoints: {
                signIn: { path: '/login', method: 'post' },
                signOut: false,
                getSession: { path: '/user-profile', method: 'get' }
            }
        }
    }
bearer-token nuxt3 nuxt-auth
1个回答
0
投票

我是@sidebase/nuxt-auth 的新成员。看起来您的身份验证模块没有以可组合状态保存您的身份验证数据,因此您已重定向回 /login 页面,因为您在配置文件中设置了 globalAppMiddleware: true 。

我还将

@sidebase/[email protected]
[email protected]
[email protected]
一起使用,但对我来说,在调用
/login
/user
端点后成功设置了身份验证状态。

nuxt.config.ts

    auth: {
        globalAppMiddleware: true,
        baseURL: `${process.env.API_BASE_URL}/api/auth`,
        provider: {
            type: 'local',
            endpoints: {
              signIn: { path: '/login', method: 'post' },
              signOut: false,
              signUp: { path: '/registration', method: 'post' },
              getSession: { path: '/user', method: 'get' }
          },
          pages: {
            login: '/login',    // page where unauthorized user will be redirect if it will try to access protected page
          },
          token: {
            maxAgeInSeconds: 60 * 60 * 24,    // token expiration 1d
            signInResponseTokenPointer: '/token',
          },
          sessionDataType: {    // user session data format
            id: 'string',
            email: 'string',
            firstName: 'string',
            lastName: 'string',
            role: 'string',
          },
        },
        session: {
          enableRefreshPeriodically: 1000 * 60 * 60,   // Refetch user session data every 1h
          enableRefreshOnWindowFocus: true,     // Refetch user session every time when browser tab will be focused
        }
      }

登录.vue

const { signIn } = useAuth()

...

await signIn({
    email: email.value,
    password: password.value
  }, {
    callbackUrl: '/',
    external: false,
  })

但是毕竟我遇到了另一个问题,承载令牌不会保存在本地存储或cookie中,因此页面刷新后用户身份验证重置并将用户重定向到/登录页面。

如果有人知道如何解决这个问题,请帮忙。

© www.soinside.com 2019 - 2024. All rights reserved.