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.
🚀 UPDATED VERSION: https://youtube.com/playlist?list=PL5S4mPUpp4OtMhpnp93EFSo42iQ40XjbF 🚀
ngl kind of mad they freaking deprecated auth helpers already.
is this approach with the react context still necessary if we use createClientComponentClient() ? 🤔
Link to repo?
Where is the "previous video" describing the "Todo" application?
So I had to put a 'use client'; on top of supabase-context.tsx or it would throw an error is that correct?
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
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 !
Helpful video
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.
where is the code?
I got session undefined error in lastest doc
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.
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!
Is it secured to use cookies this way?
Is there a public repo of this video series?
Thanks for this video Jon !
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?
Please, make a simple video using authentication with next 13 server components. It is very confusing to understand, when it includes database.
lots of thanks … next add auth with rls in frontend like admin users, editor…how do it…