Next.js App Directory: Managing Cookies and Authentication with Route Handlers

Posted by

Route Handler Cookies // Auth in the Next.js app directory

Route Handler Cookies // Auth in the Next.js App Directory

When building a Next.js app, handling authentication and managing cookies for route handlers is an important part of the development process.

Setting Up Route Handler Cookies

First, you’ll need to set up a route handler in your Next.js app. This can be done using the built-in routing system or a third-party library such as React Router. Once you have a route handler in place, you can start implementing cookies for authentication.

Storing and Retrieving Cookies

To store a cookie in Next.js, you can use the js-cookie library or the built-in document.cookie API. Here’s an example of how you can set a cookie for authentication:

        
document.cookie = "authToken=yourAuthToken; path=/;";
        
    

And to retrieve the cookie in your route handler, you can use the following code:

        
const authToken = document.cookie.replace(/(?:(?:^|.*;s*)authTokens*=s*([^;]*).*$)|^.*$/, "$1");
        
    

Handling Authentication with Cookies

Once you have set up the cookies in your route handler, you can use them to handle authentication. For example, you can check if the user has a valid authentication token in the cookie and redirect them to the login page if they don’t. Here’s an example of how you can handle authentication in your route handler:

        
if (!authToken) {
    window.location.href = "/login";
}
        
    

Conclusion

Handling route handler cookies and authentication in a Next.js app directory is an important aspect of building a secure and user-friendly application. By following the steps outlined in this article, you can effectively set up and manage cookies for authentication in your Next.js app.

0 0 votes
Article Rating
23 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Supabase
6 months ago

🚀 UPDATED VERSION: https://youtube.com/playlist?list=PL5S4mPUpp4OtMhpnp93EFSo42iQ40XjbF 🚀

@chrislauer6222
6 months ago

ngl kind of mad they freaking deprecated auth helpers already.

@user-dm5qi4nb6l
6 months ago

is this approach with the react context still necessary if we use createClientComponentClient() ? 🤔

@samshon9285
6 months ago

Link to repo?

@derekjwilliams
6 months ago

Where is the "previous video" describing the "Todo" application?

@TheyCallMeFelix
6 months ago

So I had to put a 'use client'; on top of supabase-context.tsx or it would throw an error is that correct?

@taewookim2340
6 months ago

Supabase looks so promising with your well-made videos! I'm learning not only Supabase, but also web dev in general with your videos. Thank you

@MrGuysmo92
6 months ago

Thanks for this video.
Please show us how to correctly handle redirect with the middleware when we are using signIn with provider. I saw so many posts about people struggling with !

@shankrunch
6 months ago

Helpful video

@alext5497
6 months ago

If i'm wrapping my entire app in a context provider, i'm killing any advantage of having an SSR application. I understand this is a supabase tutorial and not a next js tutorial but theres gotta be a better way.

My assumption was that everything would just be stored in JWT or sessions on a cookie.

@fraternitas5117
6 months ago

where is the code?

@honwicong3760
6 months ago

I got session undefined error in lastest doc

@zhawtof
6 months ago

Does anyone have a link to the bug Jon references at 10:02? We're excited to implement this, but hoping that the bug is resolved soon.

@thesunabsolute
6 months ago

Hey Jon, love these videos. Any way we can get an example of a protected page added to the github example repo? The docs point to it, but I'm not seeing any logic to protect the page in the code. Thanks!

@RobinGiel
6 months ago

Is it secured to use cookies this way?

@RobbieCrenshaw
6 months ago

Is there a public repo of this video series?

@codewithguillaume
6 months ago

Thanks for this video Jon !

@venicebeachsurfer
6 months ago

Lets say we wanted to experiment with other ways, rather than context – "how" would we know IF we are creating more than one supabase instance? Is there some diagnostic we could use/run/view, to know this?

@darius5979
6 months ago

Please, make a simple video using authentication with next 13 server components. It is very confusing to understand, when it includes database.

@aramabdulrahman2087
6 months ago

lots of thanks … next add auth with rls in frontend like admin users, editor…how do it…