When trying to add an auth exchange to my urql client, it gets run on the server when the app starts and on the client subsequent times until refresh. The problem is in my getAuth function, which is as follows:
const getAuth = async ({ authState }) => {
const token = localStorage.getItem('5etoken');
if (!authState) {
if (token) {
return { token };
}
return null;
}
if (token) {
const decoded = jwt.decode(token) as jwt.JwtPayload;
if (decoded.exp !== undefined && decoded.exp < Date.now() / 1000) {
return { token };
}
}
return null;
};
When I run my app, I get an error saying localStorage is undefined. If I check that the function is running in the browser, then my token never gets set on app start and I'm logged out when I refresh the page, so I can't use that approach. I've tried multiple approaches:
- Using dynamic imports with
ssrset to false - Creating the client in a useEffect hook
- Using
next-urql'swithUrqlClientHOC only using the auth exchange when in the browser
None of what I tried worked and I'm running out of ideas.
I eventually figured out that
createClientwas being called on the server side. I managed to force it to run in the browser by creating the client in auseEffecthook. I'm not sure why creating it in auseEffectdidn't work months ago.