I don't know if it is because I'm starting to get a handle on Next.js or you just have a way of explaining things so people like me understand but I now understand Middleware. Yours is not the first video I've seen on this topic but this one was the first one that I understood. LOL. Thanks
Seriously, it keeps changing, and there doesn't seem to be any way to automatically persist cookies between requests for a single login session for example
Josh any chance on you making a video on how to setup a cookie consent in next? There is very few information about how to make your website grpd ready
I had the same problem when integrating next-intl and next-auth next-intl's documentation is 1 export default and recommend using client component, but useLocale function not working in middlware Moreover in the new version NextResponse.redirect('dashboard',request.url) can't be replaced with NextResponse.rewrite('dashboard',request.url) but it only changes the content without changing the path url on the browser this creates a bad user experience because the user doesn't really know if this is the content of the page they want to visit or it's not easy to confuse because the url is the same but the content is coming from another page Anyone with ideas please message me
Do you have something on combining for example (highly appreciated) next-intl and next-auth , and just in general how to combine different middleware especially in nextjs 13.2+ ?
Around about 10m 30s the auth cookie is deleted in the middleware and yet remains in the browser and I assume so would the cookie value if Josh hadn't manually deleted it. Why is this and does it mean the user will still possess any authorisation provided by the 'deleted' cookie?
My current set up is my server stored an access token and refresh token in cookies on the BE. My API will return the jwt access token on login and then I’m using js-cookie to save the cookie, then I can use that cookie in middleware. Lots of cookies 🍪 is js-cookie a fine library for creating the cookie on the client side?
@@joshtriedcoding how do you usually handle protected pages where you get a signed jwt access token in the response from server after a successful login? I store the jwt in a cookie using "cookies-next" and then in the middleware, I read the jwt from the cookie, pass it in the auth header to an api call which verifies the jwt and based on the response, I either redirect to login or let the request pass through the middleware. Is this the correct way to protect routes in your nextjs app?
@@AyushSethGuitarCovers I don't think there's a right and wrong way in that sense. It would probably be easier for you to verify the JWT in the middleware already, as that would be faster and you wouldn't need to remember to protect each route, as with the middleware you can match certain paths. For example, you could protect all paths containing "api" in them, saving you time and making your code easier to maintain
Anyone know how to remove cookies from the headers , for example when creating a sign out function? I'm using the clear() method on NextRequest.cookies, and getting an error saying that it's not a function
i don’t get why the cookie can’t be fully deleted. it’s able to be mutated but not deleted?? why?
0:45 middleware
5:10 get cookie
8:02 get all cookies
9:58 delete cookie
10:43 set cookie
12:43 headers
15:20 eg use case
I don't know if it is because I'm starting to get a handle on Next.js or you just have a way of explaining things so people like me understand but I now understand Middleware. Yours is not the first video I've seen on this topic but this one was the first one that I understood. LOL. Thanks
handling requests and responses is a MESS in NextJs
Seriously, it keeps changing, and there doesn't seem to be any way to automatically persist cookies between requests for a single login session for example
7:18 as of January 2023, this Nextjs' method actually does work.
what works now?
@@emilianofz2502 the documentation by NextJs works.
Josh any chance on you making a video on how to setup a cookie consent in next? There is very few information about how to make your website grpd ready
I had the same problem when integrating next-intl and next-auth next-intl's documentation is 1 export default and recommend using client component, but useLocale function not working in middlware
Moreover in the new version NextResponse.redirect('dashboard',request.url) can't be replaced with NextResponse.rewrite('dashboard',request.url) but it only changes the content without changing the path url on the browser this creates a bad user experience because the user doesn't really know if this is the content of the page they want to visit or it's not easy to confuse because the url is the same but the content is coming from another page
Anyone with ideas please message me
IDK what it is about your videos I love, But I gotta say you're rocking!!! Keep on the Good job bro 😘
Do you have something on combining for example (highly appreciated) next-intl and next-auth , and just in general how to combine different middleware especially in nextjs 13.2+ ?
I have a same issue when integrating with next-intl and next-auth
@@ICOReviewtoken i left nextauth in pages for a while.
crazy to me how next.js docs are giving wrong syntaxe, just wow, thanks for the vid josh
Since you're in germany too, could you show how to handle cookie consent and/or google tag manager in nextjs 13? Thanks for all you do!
is it possible to redirect user to the login page if a cookie let's say called 'redirect' exists?
can't we set cookie in api folder instead of middleware
bro the method of the docs works for me !
cookies() destroys ssg for entire project?
Around about 10m 30s the auth cookie is deleted in the middleware and yet remains in the browser and I assume so would the cookie value if Josh hadn't manually deleted it. Why is this and does it mean the user will still possess any authorisation provided by the 'deleted' cookie?
It's deleting it on the server in the scope of the Middleware
Thanks 🎉 from Paraguay 🇵🇾
And how could you access the cookie from the api/route?
greetings!!
Yes, that's what I want to know
@@ohmegatech666 It helped me to use the set Cookies method from the cookies-next library.
to access/set/delete cookies in the api folder.
Greetings
via cookies function from next/headers
Time to hook it with an express api and jwt;
Thanks buddy.
Lets goo
Love your vid. Very informative.
Thanks, glad to hear that
Thanks buddy you saves my time ✌️
josh, where is your course?
My current set up is my server stored an access token and refresh token in cookies on the BE.
My API will return the jwt access token on login and then I’m using js-cookie to save the cookie, then I can use that cookie in middleware.
Lots of cookies 🍪 is js-cookie a fine library for creating the cookie on the client side?
Not sure, I never generate the cookies client-side. I always return them in the response from the server
@@joshtriedcoding how do you usually handle protected pages where you get a signed jwt access token in the response from server after a successful login? I store the jwt in a cookie using "cookies-next" and then in the middleware, I read the jwt from the cookie, pass it in the auth header to an api call which verifies the jwt and based on the response, I either redirect to login or let the request pass through the middleware. Is this the correct way to protect routes in your nextjs app?
@@AyushSethGuitarCovers I don't think there's a right and wrong way in that sense. It would probably be easier for you to verify the JWT in the middleware already, as that would be faster and you wouldn't need to remember to protect each route, as with the middleware you can match certain paths. For example, you could protect all paths containing "api" in them, saving you time and making your code easier to maintain
much appreciated concise video!
Mine came as an object so yes they were correct
Thank you.
Anyone know how to remove cookies from the headers , for example when creating a sign out function? I'm using the clear() method on NextRequest.cookies, and getting an error saying that it's not a function
Set it to null or undefined
Also I think there is a delete method
I can't see
duuuude
Great video...Josh! Can you share your github
for sure, here it is: github.com/joschan21
@@joshtriedcoding Thanks man
what if we have dynamic route n want to protect them also how to ? req.nextUrl.pathname.startsWidth(/dashboard/*) like this ?