Nuxt.js auth 用户已设置但已登录仍为 false

问题描述

带有 Nuxt Auth 的 js 应用程序以及当我想登录时。未设置用户-user_id__countloggedIn

false

所以我写了这个,然后设置了用户,但 const response = await this.$auth.loginWith('local',{ data: { email: this.form.email.value,password: this.form.password.value,} }); this.$auth.setUser(response.data.user); this.$auth.strategy.token.set(response.data.jwt.access_token) this.$auth.strategy.refreshToken.set(response.data.jwt.refresh_token) 仍然是假的。这是我的loggedIn

nuxt.config.js

你能帮我吗?

解决方法

我找到了答案的解决方案。我自己写了scheme

import { RefreshScheme } from '~auth/runtime'

export default class CustomScheme extends RefreshScheme {
  // Override `fetchUser` method of `local` scheme
  async fetchUser (endpoint) {

    this.options.endpoints.user = {
      ...this.options.endpoints.user,data: {
        token: this.$auth.strategy.refreshToken.get()
      }
    }

    // Token is required but not available
    if (!this.check().valid) {
      return
    }

    // User endpoint is disabled.
    if (!this.options.endpoints.user) {
      this.$auth.setUser({})
      return
    }

    // Try to fetch user and then set
    return this.$auth.requestWith(
      this.name,endpoint,this.options.endpoints.user
    ).then((response) => {
      const user = response.data.user

      // Transform the user object
      const customUser = {
        ...user,fullName: user.name,roles: ['user']
      }

      // Set the custom user
      // The `customUser` object will be accessible through `this.$auth.user`
      // Like `this.$auth.user.fullName` or `this.$auth.user.roles`
      this.$auth.setUser(customUser)

      return response
    }).catch((error) => {
      this.$auth.callOnError(error,{ method: 'fetchUser' })
    })
  }
}

显然是从 propertyName 获取用户的问题。我用我自己的响应 const user = response.data.user 替换了代码,现在它似乎工作了 :)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...