My current struggle involves using next js
. Whenever I attempt to utilize localStorage
on my page, an error pops up stating that localStorage is not defined
. After scouring through numerous articles, the common advice is to switch to window.localStorage
, but unfortunately, this also triggers an error mentioning window is not defined
. This issue has left me feeling incredibly frustrated. The specific area where I am trying to implement localStorage
is within the serverSideProps
function:
export async function getServerSideProps(context)
let user = {
user:"",
isAuth:""
};
const response = await fetch(
`${server}/api/user/getUserData`, {
method: "POST",
headers: {
"mode": "cors",
'Content-Type': 'application/json',
'auth-token': localStorage.getItem('token')
},
body: JSON.stringify({ username })
});
const result = await response.json()
.then(user => {
if (user["user"][0] != null) {
user.user = user["user"][0]
user.isAuth = user.isAuth;
}
else {
user.isAuth = 'false';
}
})
.catch(err => console.log(err))
return {
props: { user
}, // will be passed to the page component as props
}
}
I am at a loss for finding a solution to this particular type of problem. Can anyone offer guidance or suggestions?