I'm using Passport.js for authentication. A variable is set on req, named req.user, containing the user data.
As follows, here it is the original store.js:
const nuxtServerInit = ({ commit }, { req }) => {
if (req.user) {
commit('SET_USER', req.user); // I got the user data, as expected
}
};But I need to get more data on every page. I think the nuxtServerInit method is the ideal location for that. So again on store.js:
const nuxtServerInit = async ({ commit }, { req }) => {
if (req.user) {
commit('SET_USER', req.user);
let { data } = await axios.get('/api/account'); // To get more user related data, from another database table...
}
};On server.js:
app.get('/api/account', (req, res) => {
console.log(req.user); // Just a test...
res.json({ ok: true });
});I got no user response on the log, when I called it from nuxtServerInit. I've also tried to get this user response from fetch method:
async fetch ({ store, params, req, res }) {
console.log('fetch req.user', req.user); // I got the user with success here
console.log('fetch store.state.user', store.state.user); // I got the user with success here too
let { data } = await axios.get('/api/account');
},Sadly, the result is the same (none) when I call req.user on /api/test from fetch. But when I try to call /api/test from mounted...
async mounted() {
let { data } = await axios.get('/api/account');
},...I got the user with success. The problem is that it happens after page rendering and I wanted it before it.
I don't know why req.user is passed on mounted, but not passed on nuxtServerInit or fetch methods. I don't know if it is a bug or if it is an intentional thing. Maybe I'm not thinking clearly. I'll appreciate any help with this question.