Cookies in NextJS: Easy To Learn & Super Useful!

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 พ.ย. 2024

ความคิดเห็น • 52

  • @samifouad
    @samifouad 6 หลายเดือนก่อน +5

    i don’t get why the cookie can’t be fully deleted. it’s able to be mutated but not deleted?? why?

  • @charlottetowelll
    @charlottetowelll หลายเดือนก่อน

    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

  • @vernevens1598
    @vernevens1598 5 หลายเดือนก่อน

    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

  • @xxapoloxx
    @xxapoloxx 6 หลายเดือนก่อน +5

    handling requests and responses is a MESS in NextJs

    • @ohmegatech666
      @ohmegatech666 4 หลายเดือนก่อน

      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

  • @kirkrepository
    @kirkrepository ปีที่แล้ว +4

    7:18 as of January 2023, this Nextjs' method actually does work.

    • @emilianofz2502
      @emilianofz2502 ปีที่แล้ว

      what works now?

    • @kirkrepository
      @kirkrepository ปีที่แล้ว +1

      @@emilianofz2502 the documentation by NextJs works.

  • @matheuscampos8097
    @matheuscampos8097 ปีที่แล้ว +7

    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

    • @ICOReviewtoken
      @ICOReviewtoken ปีที่แล้ว +1

      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

  • @riveto_ir
    @riveto_ir ปีที่แล้ว

    IDK what it is about your videos I love, But I gotta say you're rocking!!! Keep on the Good job bro 😘

  • @andriisukhariev
    @andriisukhariev ปีที่แล้ว +5

    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+ ?

    • @ICOReviewtoken
      @ICOReviewtoken ปีที่แล้ว +2

      I have a same issue when integrating with next-intl and next-auth

    • @andriisukhariev
      @andriisukhariev ปีที่แล้ว

      @@ICOReviewtoken i left nextauth in pages for a while.

  • @hatem.lamine
    @hatem.lamine ปีที่แล้ว +1

    crazy to me how next.js docs are giving wrong syntaxe, just wow, thanks for the vid josh

  • @maxwolf2718
    @maxwolf2718 ปีที่แล้ว +2

    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!

  • @christiandinata296
    @christiandinata296 9 หลายเดือนก่อน +1

    is it possible to redirect user to the login page if a cookie let's say called 'redirect' exists?

  • @saireddy5810
    @saireddy5810 ปีที่แล้ว +2

    can't we set cookie in api folder instead of middleware

  • @EPIC-ev4lx
    @EPIC-ev4lx ปีที่แล้ว +1

    bro the method of the docs works for me !

  • @en_kratia
    @en_kratia 6 หลายเดือนก่อน +1

    cookies() destroys ssg for entire project?

  • @John-eq5cd
    @John-eq5cd ปีที่แล้ว +1

    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?

    • @zaynelovecraft
      @zaynelovecraft 9 หลายเดือนก่อน +1

      It's deleting it on the server in the scope of the Middleware

  • @Devp720
    @Devp720 9 หลายเดือนก่อน

    Thanks 🎉 from Paraguay 🇵🇾

  • @julian0590
    @julian0590 6 หลายเดือนก่อน +2

    And how could you access the cookie from the api/route?
    greetings!!

    • @ohmegatech666
      @ohmegatech666 4 หลายเดือนก่อน

      Yes, that's what I want to know

    • @julian0590
      @julian0590 4 หลายเดือนก่อน

      @@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

    • @awekeningbro1207
      @awekeningbro1207 10 วันที่ผ่านมา

      via cookies function from next/headers

  • @raymondmichael4987
    @raymondmichael4987 2 ปีที่แล้ว +1

    Time to hook it with an express api and jwt;
    Thanks buddy.

  • @1000ylovers
    @1000ylovers 2 ปีที่แล้ว +2

    Love your vid. Very informative.

  • @deepitasaha195
    @deepitasaha195 ปีที่แล้ว

    Thanks buddy you saves my time ✌️

  • @cloudsss83
    @cloudsss83 2 หลายเดือนก่อน

    josh, where is your course?

  • @tylerolthuizen3347
    @tylerolthuizen3347 ปีที่แล้ว

    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
      @joshtriedcoding  ปีที่แล้ว

      Not sure, I never generate the cookies client-side. I always return them in the response from the server

    • @AyushSethGuitarCovers
      @AyushSethGuitarCovers ปีที่แล้ว

      @@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?

    • @joshtriedcoding
      @joshtriedcoding  ปีที่แล้ว

      @@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

  • @if432
    @if432 ปีที่แล้ว

    much appreciated concise video!

  • @elozinoovedhe
    @elozinoovedhe 8 หลายเดือนก่อน

    Mine came as an object so yes they were correct

  • @FasilMinale-q5g
    @FasilMinale-q5g ปีที่แล้ว

    Thank you.

  • @andreipene8647
    @andreipene8647 ปีที่แล้ว

    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

    • @LeoFernandezR
      @LeoFernandezR ปีที่แล้ว

      Set it to null or undefined

    • @LeoFernandezR
      @LeoFernandezR ปีที่แล้ว

      Also I think there is a delete method

  • @mrlectus
    @mrlectus ปีที่แล้ว

    I can't see

  • @MadRad1000
    @MadRad1000 ปีที่แล้ว

    duuuude

  • @developer_01
    @developer_01 2 ปีที่แล้ว

    Great video...Josh! Can you share your github

    • @joshtriedcoding
      @joshtriedcoding  2 ปีที่แล้ว

      for sure, here it is: github.com/joschan21

    • @developer_01
      @developer_01 2 ปีที่แล้ว

      @@joshtriedcoding Thanks man

  • @faisaltariq-un6so
    @faisaltariq-un6so ปีที่แล้ว

    what if we have dynamic route n want to protect them also how to ? req.nextUrl.pathname.startsWidth(/dashboard/*) like this ?