Currently I'm working on a login-system in my Nuxt app that has Strapi in the back-end of itself. I used nuxt-auth local schemes for my login-system, and followed this article for making that. unfortunately the version of "Strapi", "Nuxt" and "nuxt-auth" that the article had used is different from my. So I have problem with the login part. Here is the code of article:
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
}
}
}
},
But when I used that, it does not set the loggedIn property of nuxt-auth to true after login. So I read the documentation of nuxt-auth and find out that we could use setUser method to set the user after login and the auto-fetch user becomes off in this mode. Here is the code of my project:
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:
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;
}
},
},
store/index.js:
export const getters = {
isAuthenticated(state) {
return state.auth.loggedIn;
},
loggedInUser(state) {
return state.auth.user;
},
};
But the problem of my method is that although the login is successful, but the loggedInUser Vuex getters looses its value after refreshing the page, the reason is that auth.user
is set to "empty object" after refreshing the page:
Could anyone please help me that how to fix this problem if I want to continue with my own method or how to configure "nuxt.config.js" file correctly if I want to follow the method of article? Is there any special "url" that I must set in the endpoints.user.url
of nuxt-auth when my back-end is Strapi?