DEV Community

Discussion on: Detecting a User's Authenticated State Client-Side in Next.js using an HttpOnly Cookie and Static Optimization

Collapse
 
kiliman profile image
Kiliman

Thanks for the example. I'm in the process of adding Firebase authentication.

I noticed that /api/checkAuth was being called on every page render. By adding [isAuthenticated] as a dependency in useEffect(), it only calls checkAuth on initial load/refresh and login/logout.

Collapse
 
justincy profile image
Justin

Actually, having an empty array [] makes it run only once. See the big yellow note that explains this at the end of the optimization section in the React hook docs.

Collapse
 
kiliman profile image
Kiliman

Hmm. Interesting. I'm aware of that.

For some reason though, it was calling checkAuth every time I navigated to a new page. So I guess the question is, is that the expectation?

Anyway, thanks again. I've expanded on your sample to use Firebase authentication. Also instead of having to mark each page as requiring authentication or not, I default to require authentication and have a withoutAuth HOC for those pages that are meant to be public.