问题描述
我尝试通过nuxt-auth模块进行登录.作为响应,我得到了令牌,然后传递了用户数据.但是,this.$Auth.loggedIn
是false
,this.$Auth.user
是undefined
.我已经战斗了3天,无法继续前进.希望有人能帮助我.
I try to do a login via nuxt-auth module. As a response I get the token and then the user data is delivered. However, this.$Auth.loggedIn
is false
and this.$Auth.user
is undefined
. I have been fighting for 3 days and can not get any further. Hope somebody can help me.
登录
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": "[email protected]",
"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"
}
推荐答案
好了一段时间,我终于解决了.问题是auth.fetchUser()
在用户响应中需要属性user
,而我的用户响应中不存在此属性.我在nuxt.config.js中将propertyName设置为false,现在可以使用
Ok after a long try, I finally solved it. The problem was that auth.fetchUser()
requires a property user
in the user response, which is not present in my user response. I set the propertyName to false in the nuxt.config.js and now it works
* 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',
propertyName: false // <--- Default "user"
}
}
}
}
}
当前我不确定这是否是模块故障
Currently I am not sure if this is a fault in the module
这篇关于Nuxt Auth-未设置用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!