【发布时间】:2021-09-05 21:47:21
【问题描述】:
当我从 nuxtjs 发送登录请求时,我在后端使用 laravel/passport 进行身份验证,并使用 nuxtjs 作为前端,如果登录成功,我会返回一个令牌和用户信息作为响应,然后用户将是重定向到/profile 页面,但是在/profile 页面中,当我返回this.$auth.loggedIn 时,我得到false!
login.vue
<script>
export default {
data() {
return {
auth: false,
email: "",
password:""
}
},
methods: {
async login() {
try {
const data = { email: this.email, password: this.password }
await this.$auth.loginWith('local', { data:data})
.then(() => this.$router.push('/profile'))
} catch (e) {
}
}
}
}
</script>
profile.vue
<template>
<div class="mt-6">loggedInUser: {{ loggedInUser }}</div>
</template>
<script>
export default{
data() {
return {
loggedInUser:this.$auth.loggedIn
}
}
}
nuxt.config.js
auth: {
strategies: {
provider: 'laravel/passport',
local: {
user: {
property: false,
autoFetch: true
},
endpoints: {
login: { url: '/login', method: 'post', propertyName: 'token' },
user: { url: '/user', method: 'get' }
},
clientId: 'cleint_id',
clientSecret: 'client_secret'
}
}
},
modules: [
'@nuxtjs/axios',
// https://go.nuxtjs.dev/bootstrap
'bootstrap-vue/nuxt',
'@nuxtjs/auth',
],
axios: {
baseURL: "http://prostudent.test/api"
},
既然登录发生在后端,那么 nuxt 如何知道用户已登录?
这是直接在我点击登录后,我被重定向到个人资料,登录的响应如预期的那样,消息、令牌和信息,但在 /profile 页面中似乎我没有登录!
【问题讨论】:
-
您的商店中
auth.loggedIn的价值是多少?检查您的开发工具。 -
nuxt/auth为你创建一个 vuex 状态。所以你实际上仍然有它的价值。 -
@kissu 我没用vuex,我的商店是空的,如果我没听错的话
-
@kissu 这是假的,用户也未定义