Question: I am facing an issue with my middleware files. More specifically, I have a file named authenticated.js. In this file, I have a function that checks for authentication.
Here is the code snippet:
export default function (context) {
//console.log('context.store.state :>> ', context.store.state);
if(process.server === true) { //Server Side
//why is my store state here is always empty when access in SSR
console.log(context.store.state.authen.UserToken);
} else { //Client Side
if(!context.store.state.authen.UserToken) {
context.redirect('/error');
} else {
//Refresh Token
context.$axios.get("authen/refresh")
.then((res) => {
const payload = {
UserToken: res.data.DataReturn.Token,
UserId: res.data.DataReturn.UserId,
UserEmail: res.data.DataReturn.UserEmail,
UserFullname: res.data.DataReturn.UserFullname
};
context.store.dispatch("authen/SetLogin", payload);
})
.catch(function (error) {
context.redirect('/error');
});
}
}
}
The problem I am facing is that the state is always empty when accessing in SSR. Why is this happening? I can access the store if the call is from the client side. Any suggestions or solutions are appreciated.