---
title: "Discussion on \"Client-Side Authentication in Next.js using Cookies\" | Hashnode"
description: "Discussion on \"Client-Side Authentication in Next.js using Cookies\". A step-by-step guide to managing client-side authentication with cookies from the client-side in Next.js"
url: "https://hashnode.com/posts/client-side-authentication-in-nextjs-using-cookies/64c7bf528d1be10c482649ae"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Client-Side Authentication in Next.js using Cookies](https://blogs.shenyien.cyou/client-side-authentication-in-nextjs-using-cookies?utm_source=hashnode&utm_medium=feed)

A step-by-step guide to managing client-side authentication with cookies from the client-side in Next.js

Jul 31, 2023 · 17 min read · blogs.shenyien.cyou  
Updated Apr 18, 2024  
Read the article

-   [#nextjs](https://hashnode.com/tag/nextjs)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#authentication](https://hashnode.com/tag/authentication)
-   [#react-query](https://hashnode.com/tag/react-query)
-   [#tutorial](https://hashnode.com/tag/tutorial)

## Written by

[Hoh Shen Yien](https://hashnode.com/@ShenYien)

@ShenYien · Kuala Lumpur, Malaysia

  
[View profile](https://hashnode.com/@ShenYien)

I write codes and blogs 🍵

My name is Hoh Shen Yien, I'm a Malaysian fullstack developer who likes to read and write sometimes 🤩

## Responses 1

Markdown supported · ⌘↵ to post

[Jason Chen](https://hashnode.com/@jason60810)

[Aug 5, 2023](https://hashnode.com/posts/client-side-authentication-in-nextjs-using-cookies/64c7bf528d1be10c482649ae/comment/64cd923413daa5cdc4003f83)

Hi, I enjoy your posts, always clear and insightful!😎

In the code snippet with `useEffect`, I wondered if missing dependencies could cause unexpected behavior?

```
useEffect(() => {
  if (!isAuthenticated && !currentUser.isLoading) {
    router.push("/"); // or login page
  }
}, 
// missing isAuthenticated and 
// currentUser.isLoading
[Component, isAuthenticated, currentUser.isLoading]);
```

Great job, and eager for your future posts!

[Hoh Shen Yien](https://hashnode.com/@ShenYien)

[Aug 5, 2023](https://hashnode.com/posts/client-side-authentication-in-nextjs-using-cookies/64c7bf528d1be10c482649ae/comment/64cd923413daa5cdc4003f83#reply-64cdc91b3d3aabc9723f82bc)

Thanks Jason.

Regarding the missing dependencies, it's actually intended!

Rather, putting more variables in the dependencies is what brings the undesirable behavior sometimes 😅. Like in the codes, I want to run the function only when the page changes, so I could either put `router.asPath` or `Component`, and if I also include `currentUser.isLoading`, it will run an additional time, which is undesirable.

As for the `isAuthenticated`, it is not a memoized variable, so putting it in the dependencies will actually result in the `useEffect` being run for every render, which is not what we want (never put an unmemoized variable in `useEffect`).

But of course, my logic here assumes that the user will be redirected to another page after logging in, which might not always be the case.
