Nuxt身份验证模块登录失败



我正在做的是一个使用Nuxt和Capacitor的移动应用程序。

由于Nuxt Auth模块不支持oAuth身份验证的应用程序内对话框,因此我尝试将capacitor-google-auth与Auth模块混合使用。

我用谷歌正确执行oAuth登录,我可以获得凭据。

然后我强迫Auth模块以这种方式(按照他们的文档(将其策略设置为谷歌

// method 1
this.$auth.$storage.setUniversal('strategy', 'google')
this.$auth.strategy.token.set(token)
const user = await this.$auth.fetchUser()
await this.$auth.setUser(user)
// method 2
this.$auth.$storage.setUniversal('strategy', 'google')
await this.$auth.setUserToken(token)

我可以通过两种方式正确地看到对API的调用,用户第一次得到正确的提取。

不幸的是,cookie和本地存储中的令牌都没有设置,或者更好的是,它设置了一毫秒,然后被清除。。。

一旦完成该过程,try catch就会得到一个错误,它就是undefined

你知道我该怎么让它工作吗?你需要更多信息吗?

感谢

编辑:添加auth配置:

auth: {
    resetOnError: true,
    localStorage: false,
    cookie: {
      prefix,
      options: {
        maxAge,
        secure: true,
        domain,
      },
    },
    redirect: {
      login: '/',
      logout: '/',
      home: '/home',
    },
    strategies: {
      local: {
        endpoints: {
          login: {
            url: '/auth/local',
            method: 'POST',
            propertyName: 'token',
          },
          logout: { url: '/auth/logout', method: 'POST' },
          user: { url: '/me', method: 'GET', propertyName: false },
        },
        tokenRequired: true,
        tokenType: 'Bearer',
      },
    },
  },

我意识到你用谷歌OAuth2授权你的用户,并从谷歌接收一个令牌,你想存储它。我也有同样的问题;注册用户后,我想让用户登录。因此,在我的情况下,我从注册API接收令牌,然后使用nuxt-auth来设置令牌。

这是用户注册后我的代码:

try {
    this.$bvToast.hide('register-error-toast')
    let response = await this.$axios.post('/api/auth/register/', this.user_data)
    // these two methods are what you can try
    this.$auth.setUser(response.data.user)
    this.$auth.setUserToken(response.data.token)
} catch (error) {
}

这是我在nuxt.config.js文件中的auth配置:

auth: {
    redirect: {
        login: '/auth/login',
        logout: '/',
        home: '/'
    },
    cookie: {
        options: {
            maxAge: 12 * 24 * 60 * 60
        }
    },
    strategies: {
        local: {
            autoFetchUser: false,
            token: {
                property: 'token',
                type: 'Bearer'
            },
            user: {
                property: 'user',
                autoFetch: false
            },
            endpoints: {
                login: {
                    url: process.env.BASE_URL + '/api/auth/login/',
                    method: 'post'
                },
                user: false,
                
            }
        }
    }
}

我和你分享了我的整个配置,它可能有一些多余的数据;您可以根据自己的需求更改配置。

相关内容

最新更新