Client and Server Side Cookies in Next.js

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

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

  • @KT-ut9zg
    @KT-ut9zg 3 ปีที่แล้ว +9

    Great explanation. I'm coming from PHP to React via Next JS. Was really struggling with storing secure cookies server-side with Next JS and not getting the answers I needed. This was clear and concise. Thanks for that and I am definitely going to buy that course.

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

      Nice!! I also come from PHP :) Spent a lot of time with CakePHP back in the day. I hope you enjoy the course!

  • @Endrit719
    @Endrit719 3 ปีที่แล้ว +9

    man, I was having so much trouble to understand authentication, thank you so much, you deserve more views

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

      Sweet!! That's the best... glad I can "unlock" some difficulties

    • @paulcullen5712
      @paulcullen5712 3 ปีที่แล้ว

      i guess I'm kinda off topic but does anyone know a good place to stream new tv shows online ?

    • @azariahdenver3450
      @azariahdenver3450 3 ปีที่แล้ว

      @Paul Cullen i watch on flixzone. You can find it on google =)

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

    well explained, alot of developers always have difficulties explaining cookies and sessions... well supper well done

  • @AryanKhan-no8pt
    @AryanKhan-no8pt 2 ปีที่แล้ว

    The best way to SET and REMOVE the cookie on Server Side...Thank you soo much 🤜🤛

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

    I was struggling with cookies-next for almost 2 hours. js-cookie n you saved my like. Ty

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

    after hours of research this video solved my confusion Thank you for your help!

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

    Where have you been all my life :))) thank you so much!!!

  • @sthefanocarvalho2823
    @sthefanocarvalho2823 3 ปีที่แล้ว +3

    Your content is awesome!
    Most of what I've learned about NextJS was in your channel :)
    Thank you!

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

      Thank you Sthefano!! Glad I can help a little bit :)

  • @manukyanq
    @manukyanq 3 ปีที่แล้ว +3

    Actual tutorials, thank you 😍

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      You’re very welcome Artsvi!

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

    Huge thanks for posting this Leigh - video came just as I was beginning to ask questions on this! Thanks for continuing to put out awesome content!

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      Thank you Terry! I hope the vid helps :D

  • @Prashantkumar-sz2qk
    @Prashantkumar-sz2qk 3 ปีที่แล้ว

    man i was stuck on bug from 2 weeks and this video helped me resolve it.

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      Nice!! Glad you got it figured out!

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

    Thanks a lot dude! I was actually having a problem to delete a httpOnly cookie and your video make it clear to me!

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

      Sweet! That's awesome :) The key is that it has to be deleted server side... sorta annoying but I guess that's the point to an httpOnly cookie haha.

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

      @@leighhalliday hey , Actually am following some issue in deleting cookie. It gets deleted when I working on my localhost, but once I deployed my website to vercel cookie is not getting deleted. Can you please help me out with it ?

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

    Hi. When you talking about the server-side cookies, What means that the browser can't read the cookies? Probably you can't get them from js-client, but we can still read them from the devtools. I think that I didn't get this security part

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

    Hi! I very appreciate you for doing all these tutorials

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

      You're very welcome Vlad!! Glad you enjoyed the video :)

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

    Great explanation, is it possible for the refresh token.?

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

    This is exactly what i am looking for.thx.

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

    Thank you, king 🤴

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

    Hi, how would you do to use js-cookie from the _app file since I need to set the cookie and check in any page of the app and then need to use getInitialProps instead of getServerSideProps

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

    Thank you for your explanation. But I have a questions, can I use the same API endpoint with a mobile app? Because I'm used to use tokens in local storage.

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

    thank you for the explanation
    im wondering how i can pass that cookie back to the server for authorazation checking (i mean the secure way)

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

    Thank you Leigh!. A stupid question: i'm working on a Next js auth session. In a real scenario my cookie http only (jwt) must be visible always in my Storage_Inspector/Cookies? Or my http only cookies must NEVER be visible in the browser? I don't know if I've made myself clear

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

      Hey Berlino! HTTP only cookies aren't visible in JavaScript, but they get sent to the server with each request.

    • @berlino5563
      @berlino5563 3 ปีที่แล้ว

      @@leighhalliday thank you

  • @Chinmay_395
    @Chinmay_395 3 ปีที่แล้ว

    Sir, wonderful explanation, this was very helpful thank you.

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

    Great explanation, I really liked your calm tone and method of teaching.
    I just have one doubt. How can I read this cookie to make sure that the user does not need to login?

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

    Which cases would you just want to use local storage?

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

    Thank you Leigh

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

      Thanks Igor!! :) Glad you enjoyed it!

  • @henryho952
    @henryho952 3 ปีที่แล้ว

    hello, i just wondering how can I apply this if the API is from the django and, I cant get the token from cookie when I fetching get API from django saying unauthorized, it seems that the bearer token not set properly, but when i refresh the browser the cookie is accessible and the get API works. how can i achieved to handle token from cookie without refreshing the browser.

  • @jerviemarquez5192
    @jerviemarquez5192 3 ปีที่แล้ว

    Thank you so much Leigh !

  • @Mike-xr1fk
    @Mike-xr1fk ปีที่แล้ว

    Hi Leigh!, thank you for making this amazing course!
    I just have a question.
    I make a request to the login API and I receive the refresh token that is in the cookie and the access token that is in the response body. Now how should I handle the access token on the client side(next js)? If I store this in a global state like context API, then I don't have access to that state in the getServerSideProps function, and I can't set it in the request.header.authorization because getServerSideProps run on the server side and the state is on the client side!
    what is the right flow for this kind of case?

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

    Beginner here bro, the backend usually will setup the httponly: true? not the frontend will setup this? the frontend will just received the secure token to store directly to the cookies?

  • @6365bharath
    @6365bharath ปีที่แล้ว

    We load our app inside an under third party sites. Third party cookies are automatically blocked inside an . Does that mean i can never use frameworks like nextjs or Remix?

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

    Hello, great video
    I would like to know why are you defining the cookie in the front instead of the api/login.js ?

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

    Beginner here bro, the backend usually will setup the httponly: true? not the frontend will setup the httponly: true? the frontend will just received the secure token with httponly: true attached already to store directly to the cookies?

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

    Thanks for the example. Does this still apply now or theres a better way.

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

    I really like the way you explained. I wanted to buy your course through the "Get Access" button, but the button didn't work.

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

    How can I protect it from CSRF Attack? Please reply

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

    Thank you, but can it work if I deploy nextjs app and expressjs app to different domain?

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

    After setting token from Server side, how can I use token to check whether a client is login or not ?

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

    Explained so well, thank you so much!

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

    Very well explained, thanks!

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

    Hey Leigh, I had a question. Even if Javascript on the frontend won't be able to read the http-only cookies, we can still see its value on the browser's Developers Tools, so how does that make it safer? For example, would it be safe to store JWT Authentication tokens as http-only cookies?

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

      Hey Sayantan! It's safer because that is you, the owner of that auth cookie looking at your own cookie value. This is opposed to a random piece of javascript executing on your page being able to access the value of this cookie.

    • @sayantankarmakar4191
      @sayantankarmakar4191 3 ปีที่แล้ว

      @@leighhalliday So does that mean once I deploy my application, the users wont be able to see the set cookies?

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

      @@sayantankarmakar4191 The only person can see it is you, or anyone can use your laptop

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

    Thank you. Awesome tutorial 💪

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      Thank you Norb! Glad you enjoyed it.

  • @MaksymMinenko
    @MaksymMinenko 3 ปีที่แล้ว

    I feel like a cookie master now! Well, almost. ;)

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      I'm also craving a cookie haha... white chocolate chip macademia nut

  • @sk-uv5fm
    @sk-uv5fm 3 ปีที่แล้ว

    Great!
    I have a question about getServerSideProps.
    if router.push to the page with getServerSideProps,
    Is the page SSR?

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

      Hey! Hmmm... I think so! I think Next.js handles that for us. I'm only 95% sure though :D

  • @leanprogrammer
    @leanprogrammer 3 ปีที่แล้ว

    Great vid!
    PS: How does this work with a pre-rendered page where I don't have access to the req object?? SSR is not the fastest for most apps

  • @GnomePuntTrainerYT
    @GnomePuntTrainerYT 3 ปีที่แล้ว +3

    You're an absolute boss, Leigh! Quick question, when you checked out the api calls in the dev tools you could see the value of the token. Is that not a problem because the value was set on the server instead of set on the client (aka h4ck3rs can't manipulate your code on the server so it didn't matter that they could read the token)?

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

      Hey OAMP! It's not a problem because that's between the browser and the server, won't be visible to anyone else, especially if it's transmitted over HTTPS.

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

    Great video man

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

    Thanks you're a life saver

  • @yigitruzgaruzun7289
    @yigitruzgaruzun7289 3 ปีที่แล้ว

    thanks leigh

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

    I'm new in Next.js. I have this .ts file that detects some redirects. In other words detects some special urls in må app. Lets say I it's detects /hello-world in the url. I then want to delete a cookie on the SSR side. How would I do this. I dont know where to get the res from on the page.

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

      Hey Dennis! Hmm from your comment I’m not sure what to recommend… if you’re new to nextjs, you may enjoy my course! next.leighhalliday.com we cover server side cookies in it.

  • @rodolforodrigues648
    @rodolforodrigues648 3 ปีที่แล้ว

    awesome content, helped me a lot!! thanks!

  • @sushantkumar3861
    @sushantkumar3861 3 ปีที่แล้ว

    I am unable to get the cookies in _app.js. How can I get it there?

  • @AllahomAnsorGaza
    @AllahomAnsorGaza 3 ปีที่แล้ว

    how read token from _app to setUser Login

  • @ginger-viking
    @ginger-viking 3 ปีที่แล้ว +1

    Love the video, thanks!

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      Thank you Ginger Viking... great name by the way!

  • @LuisVazquez-ky7yb
    @LuisVazquez-ky7yb 3 ปีที่แล้ว

    So i am using axios on frontend and I see you are returning the session token with getServerSideProps. Now my question is how do i send this to axios do i need to pass this token with all the children which will be hell for me. Can i store this token on a context or is also a bad way of doing? or can I create a function to fetch the token and then call axios. Please let me know which is the best way of doing this

  • @Emraan-Shaikh
    @Emraan-Shaikh 3 ปีที่แล้ว

    Quick question,
    incase of isomorphic application development after login cookies set at client-side and API used for login is not belongs to same domain, how to cover this scenario please advise

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      I don't think you can have HttpOnly cookies across multiple domains unfortunately... unless I'm wrong?

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

    i do this , but in production i have problems

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

    Is there a size limit on server side cookies?

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

    Thanks for helping 💯

  • @elisson357
    @elisson357 3 ปีที่แล้ว

    Thanks for sharing this.

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

    How to use it with express-session and witgout nextjs api routes ?

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

      Hey! Sorry... I don't have an answer for you :) No clue! I have only used express with Next.js one time and it didn't have any sessions.

    • @mydisk2859
      @mydisk2859 3 ปีที่แล้ว

      @@leighhalliday Sir actually i am running express an next.js on different servers even if you could refer any idea how i could save cookie from api to next.js or any reference would be helpful...

  • @ArunJenson-r5u
    @ArunJenson-r5u ปีที่แล้ว +1

    how to set the cookie in serverside in next 13 app router

  • @alexkey9372
    @alexkey9372 3 ปีที่แล้ว

    Great tutorial! how would you authenticate though certain routes? (without next-auth). I'm trying to do the authentication with wordpress and its rest api but really struggle.. id like also to avoid serversideprops as it would make my whole project slow again

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

      Hey Alex! Hmm... I suppose you could add a client side check that confirms you have access before anything at all is rendered... but I think either way adding validation adds a slight slowdown. If you can use a cookie with confidence, I don't think getServerSideProps would slow you down too much... we're probably talking < 100ms.

    • @alexkey9372
      @alexkey9372 3 ปีที่แล้ว

      @@leighhalliday em.. having seen the difference, I'd say def it does slows down significantly. I mean right, now I'm authenticating user with user credentials generating a jwt (having the jwt plugin there) and when credential match on wordpress then storing the jwt info on httponly cookie. But how to access this on the client without serverprops? Also, not sure if this is the best approach.. what would you recommend? :) was thinking about passport library etc but not sure.

  • @bakbak1830
    @bakbak1830 3 ปีที่แล้ว

    Hi, i setup the cookie using login api route as shown in the video. I want to send the jwt token from client end to do some queries and mutations on the server. The cookie is not available on client end to do that operation. One way is to get the cookie on getServerSideProps and pass it onto the component or the other way is to store it locally using local storage or client side cookie. What will be your suggestion? Is there any other way out?

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

      Hey Bak! If the server is on the same domain it'll be passed automatically to the backend with the request. If it's a different domain, I think you'll have to store the token somewhere client-side so you can add it in as a header.

    • @bakbak1830
      @bakbak1830 3 ปีที่แล้ว

      @@leighhalliday thanks for the reply. It is clear; will test it out.

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

    It worked thankyou so much

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

    Ty, really helpful.

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

      Thanks Juan! Glad you enjoyed it!

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

    Thank you so much!

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

    awesome!

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

    Can anyone manipulate server side cookies.

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

      If you have it as an http only cookie, nope

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

    thank you

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

    Thanks

  • @pedroapfilho
    @pedroapfilho 3 ปีที่แล้ว

    What if you want to fetch data on the server with these cookies? How would it work?

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      Hey Pedro! That’s exactly what I did in getServerSideProps, that runs server side. You’d access cookies in the exact same way in an API page function

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

    bro you saved my ass thanks

  • @kurniawanhendra2683
    @kurniawanhendra2683 3 ปีที่แล้ว

    Hi Hall, this is hendra from Indonesia. Is your course available on Udemy ? Your course is looks great, covered lots of stuff...

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

      Hey Kurniawan! No, it's not on Udemy but you can get a purchasing power parity code here to make the price more reasonable for you next.leighhalliday.com/ppp

  • @dhruvpahwa7602
    @dhruvpahwa7602 3 ปีที่แล้ว

    I'm getting this error on logout: Cannot set headers after they are sent to the client

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

      Hmm... double check to ensure your backend doesn't have any errors... that's something I've noticed can cause it.

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

      @@leighhalliday Yeah, I was sending res.json without retuning it

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

    thank's alot

  • @Julia_Berrrlin
    @Julia_Berrrlin 3 ปีที่แล้ว

    I'm so confused how the node server works in next.js. it's all single files for any route? What about secret routes?
    Watching the entire app to help you :)

    • @leighhalliday
      @leighhalliday  3 ปีที่แล้ว

      Hey Julia! Each one runs as a serverless function when it's deployed. You'd have to write code to guard access if certain routes are to be "secret"... by secret you mean authorized users only, right?

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

    js-cookis return undefin

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

    I think you should comment in your video that this is not how a login or logout should work and to be honest, I think using login and logout as the example was suboptimal. Someone new to all this might get a totally wrong impression. Good video otherwise.