4er*_*ern 1 authentication axios vuex nuxt.js
我尝试通过nuxt-auth模块进行登录。作为响应,我得到了令牌,然后传递了用户数据。但是,this.$Auth.loggedInis false和this.$Auth.useris undefined。我已经战斗了3天,无法继续前进。希望有人能帮助我。
登录
await this.$auth.login({
data: {
email: this.email,
password: this.password
}
}).then(() => {
this.$router.push('/dashboard')
}).catch(err => {
this.snackbar.show = true;
})
Run Code Online (Sandbox Code Playgroud)
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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
响应登录
{
"access_token": "xxxxxxxxxxxxx.eyJpc3MiOiJodHRwczpcL1wvYXBpLmFwcHJlexxxxxxxcxXRoXC9sb2dpbiIsImlhdCI6MTUzODI5NTczMywiZXhwIjoxNTM4Mjk5MzMzLCJuYmYiOjE1MzgyOTU3MzMsImp0aSI6ImdtWWVyZTViQjk1cU5BRG8iLCJzdWIiOjIsInBydiI6IjYwODM2NzQ0MzQ4ZDQzMTk4NzE4N2ZjMWM2YzIzMjYxMDcyMWE5ZjAifQ.JhOiwIg7StzZR71aqYyI9rJpPXVclmddzPSIwqCIUN4",
"token_type": "bearer",
"expires_in": 3600
}
Run Code Online (Sandbox Code Playgroud)
回应使用者
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
bar*_*5um 14
更新 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' }
}
}
}
},
Run Code Online (Sandbox Code Playgroud)
经过长时间的尝试,我终于解决了。问题在于,用户响应中auth.fetchUser()需要一个属性user,而我的用户响应中不存在该属性。我在nuxt.config.js中将propertyName设置为false,现在可以使用了
* 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"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
目前,我不确定这是否是模块故障
| 归档时间: |
|
| 查看次数: |
1990 次 |
| 最近记录: |