Nuxt Auth-未设置用户数据

时间:2018-09-30 08:32:56

标签: authentication axios vuex nuxt.js

我尝试通过nuxt-auth模块进行登录。作为响应,我得到了令牌,然后传递了用户数据。但是,this.$Auth.loggedInfalse,而this.$Auth.userundefined。我已经战斗了3天,无法继续前进。希望有人能帮助我。

登录

await this.$auth.login({
    data: {
        email: this.email,
        password: this.password
    }
}).then(() => {
    this.$router.push('/dashboard')
}).catch(err => {
    this.snackbar.show = true;
})

nuxt.config.js

auth: {
    strategies: {
        local: {
            endpoints: {
                login: {
                    url: '/auth/login',
                    method: 'post',
                    propertyName: 'access_token'
                },
                logout: {
                    url: '/auth/logout',
                    method: 'post'
                },
                user: {
                    url: '/auth/me',
                    method: 'post'
                },
                tokenRequired: true
            }
        }
    }
}

响应登录

{
"access_token": "xxxxxxxxxxxxx.eyJpc3MiOiJodHRwczpcL1wvYXBpLmFwcHJlexxxxxxxcxXRoXC9sb2dpbiIsImlhdCI6MTUzODI5NTczMywiZXhwIjoxNTM4Mjk5MzMzLCJuYmYiOjE1MzgyOTU3MzMsImp0aSI6ImdtWWVyZTViQjk1cU5BRG8iLCJzdWIiOjIsInBydiI6IjYwODM2NzQ0MzQ4ZDQzMTk4NzE4N2ZjMWM2YzIzMjYxMDcyMWE5ZjAifQ.JhOiwIg7StzZR71aqYyI9rJpPXVclmddzPSIwqCIUN4",
"token_type": "bearer",
"expires_in": 3600
}

响应用户

{
    "id": 2,
    "name": "Dominik Dummy",
    "email": "dummy@andreas-pabst.de",
    "created_at": {
        "date": "2018-09-28 09:11:31.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    "updated_at": {
        "date": "2018-09-28 09:11:31.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    "self": "https:\/\/api.apprex.de\/api\/users\/2"
}

enter image description here

3 个答案:

答案 0 :(得分:13)

更新 auth-next": "^5.0.0"

您必须设置 property: false 而不是 propertyName。 所以你的 nuxt.config.js 可能是这样的:

auth: {
    strategies: {
      local: {
        token: {
          property: 'access_token',
          required: true,
          type: 'Bearer'
        },
        user: {
          property: false, // <--- Default "user"
          autoFetch: true
        },
        endpoints: {
          login: { url: 'api/login', method: 'post' },
          logout: { url: 'api/auth/logout', method: 'post' },
          user: { url: 'api/user', method: 'get' }
        }
      }
    }
  },

答案 1 :(得分:2)

经过长时间的尝试,我终于解决了。问题是2.在用户响应中需要属性auth.fetchUser(),而我的用户响应中不存在属性。我在nuxt.config.js中将propertyName设置为false,现在可以使用

* nuxt.config.js

user

当前我不确定这是否是模块故障

答案 2 :(得分:1)

您应该调用this.$auth.fetchUser()来填充user数据,并在loggedIn存储Docs中调用auth