目前我正在我的Nuxt应用程序中开发一个登录系统,该应用程序本身的后端有Strapi 。我为我的登录系统使用了 nuxt-auth 本地方案,并按照本文进行了制作。不幸的是,文章使用的“Strapi”、“Nuxt”和“nuxt-auth”版本与我的不同。所以我的登录部分有问题。这是文章的代码:
nuxt.config.js:
auth: {
strategies: {
local: {
endpoints: {
login: {
url: 'auth/local',
method: 'post',
propertyName: 'jwt'
},
user: {
url: 'users/me',
method: 'get',
propertyName: false
},
logout: false
}
}
}
},
但是当我使用它时,它不会在登录后将 nuxt-auth的loggedIn 属性设置为 true。所以我阅读了nuxt-auth 的文档,发现我们可以在登录后使用setUser方法设置用户,并且在这种模式下自动获取用户关闭。这是我的项目的代码:
nuxt.config.js:
auth: {
strategies: {
local: {
token: {
property: false,
required: true,
type: 'Bearer'
},
user: {
property: false,
autoFetch: false
},
endpoints: {
login: { url: 'auth/local', method: 'post', propertyName: 'jwt' },
logout: false,
user: false
// { url: 'users/me', method: 'get', property: false, autoFetch: true }
}
}
}
}
login.vue 方法:
methods: {
async login() {
this.error = null;
try {
let responseBack = await this.$auth.loginWith("local", {
data: {
identifier: this.email,
password: this.password,
},
});
console.log(responseBack);
this.$auth.setUser(responseBack.data.user);
} catch (e) {
this.error = e.response.data.message[0].messages[0].message;
}
},
},
存储/index.js:
export const getters = {
isAuthenticated(state) {
return state.auth.loggedIn;
},
loggedInUser(state) {
return state.auth.user;
},
};
但是我的方法的问题是,虽然登录成功,但是刷新页面后, loggedInUser Vuex getter 值丢失,原因是刷新页面auth.user后设置为“空对象”:
如果我想继续使用自己的方法,或者如果我想按照文章的方法正确配置“nuxt.config.js”文件,谁能帮我解决这个问题?当我的后端是Strapiendpoints.user.url时,我必须在nuxt-auth中设置任何特殊的“url”吗?
