Client and Server Side Cookies in Next.js

  Рет қаралды 60,924

Leigh Halliday

Leigh Halliday

Күн бұрын

Пікірлер: 121
@Endrit719
@Endrit719 3 жыл бұрын
man, I was having so much trouble to understand authentication, thank you so much, you deserve more views
@leighhalliday
@leighhalliday 3 жыл бұрын
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 =)
@KT-ut9zg
@KT-ut9zg 3 жыл бұрын
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 жыл бұрын
Nice!! I also come from PHP :) Spent a lot of time with CakePHP back in the day. I hope you enjoy the course!
@magimaartdeveloper7465
@magimaartdeveloper7465 3 жыл бұрын
well explained, alot of developers always have difficulties explaining cookies and sessions... well supper well done
@terrylennon
@terrylennon 4 жыл бұрын
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 4 жыл бұрын
Thank you Terry! I hope the vid helps :D
@andrewiandreleonardo1765
@andrewiandreleonardo1765 2 жыл бұрын
Where have you been all my life :))) thank you so much!!!
@sthefanocarvalho2823
@sthefanocarvalho2823 4 жыл бұрын
Your content is awesome! Most of what I've learned about NextJS was in your channel :) Thank you!
@leighhalliday
@leighhalliday 4 жыл бұрын
Thank you Sthefano!! Glad I can help a little bit :)
@manukyanq
@manukyanq 4 жыл бұрын
Actual tutorials, thank you 😍
@leighhalliday
@leighhalliday 4 жыл бұрын
You’re very welcome Artsvi!
@WhiteVanyo
@WhiteVanyo 3 жыл бұрын
Thank you, king 🤴
@trevorkelley5397
@trevorkelley5397 2 жыл бұрын
after hours of research this video solved my confusion Thank you for your help!
@AryanKhan-no8pt
@AryanKhan-no8pt 2 жыл бұрын
The best way to SET and REMOVE the cookie on Server Side...Thank you soo much 🤜🤛
@trustlang_ua
@trustlang_ua 4 жыл бұрын
Hi! I very appreciate you for doing all these tutorials
@leighhalliday
@leighhalliday 4 жыл бұрын
You're very welcome Vlad!! Glad you enjoyed the video :)
@addliam
@addliam Жыл бұрын
I was struggling with cookies-next for almost 2 hours. js-cookie n you saved my like. Ty
@leandropassos6128
@leandropassos6128 3 жыл бұрын
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 жыл бұрын
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 ?
@igor_cojocaru
@igor_cojocaru 4 жыл бұрын
Thank you Leigh
@leighhalliday
@leighhalliday 4 жыл бұрын
Thanks Igor!! :) Glad you enjoyed it!
@hbela1000
@hbela1000 2 жыл бұрын
This is exactly what i am looking for.thx.
@jerviemarquez5192
@jerviemarquez5192 3 жыл бұрын
Thank you so much Leigh !
@NorbyWinner
@NorbyWinner 4 жыл бұрын
Thank you. Awesome tutorial 💪
@leighhalliday
@leighhalliday 4 жыл бұрын
Thank you Norb! Glad you enjoyed it.
@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!
@andrewpham3933
@andrewpham3933 2 жыл бұрын
Explained so well, thank you so much!
@leighhalliday
@leighhalliday 2 жыл бұрын
Thanks Andrew!
@Chinmay_395
@Chinmay_395 3 жыл бұрын
Sir, wonderful explanation, this was very helpful thank you.
@PROJECTJMUSIC
@PROJECTJMUSIC 2 жыл бұрын
Very well explained, thanks!
@rodolforodrigues648
@rodolforodrigues648 3 жыл бұрын
awesome content, helped me a lot!! thanks!
@ginger-viking
@ginger-viking 3 жыл бұрын
Love the video, thanks!
@leighhalliday
@leighhalliday 3 жыл бұрын
Thank you Ginger Viking... great name by the way!
@pulkitkakar8597
@pulkitkakar8597 Жыл бұрын
Thanks for helping 💯
@rexomiv7352
@rexomiv7352 2 жыл бұрын
Thanks you're a life saver
@VuthivannYong
@VuthivannYong Жыл бұрын
Great explanation, is it possible for the refresh token.?
@salvadorhernandezlopez953
@salvadorhernandezlopez953 2 жыл бұрын
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
@yigitruzgaruzun7289
@yigitruzgaruzun7289 3 жыл бұрын
thanks leigh
@vikhyatverma4016
@vikhyatverma4016 2 жыл бұрын
It worked thankyou so much
@elisson357
@elisson357 3 жыл бұрын
Thanks for sharing this.
@leighhalliday
@leighhalliday 3 жыл бұрын
You're very welcome! :D
@Nurtylek
@Nurtylek 4 жыл бұрын
awesome!
@leighhalliday
@leighhalliday 4 жыл бұрын
Thanks :)
@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?
@davies8183
@davies8183 3 жыл бұрын
Great video man
@leighhalliday
@leighhalliday 3 жыл бұрын
Appreciate it!
@JuanTarallo
@JuanTarallo 3 жыл бұрын
Ty, really helpful.
@leighhalliday
@leighhalliday 3 жыл бұрын
Thanks Juan! Glad you enjoyed it!
@eleukhin
@eleukhin 2 жыл бұрын
Thank you so much!
@leighhalliday
@leighhalliday 2 жыл бұрын
Glad it helped!
@dhruvpahwa7602
@dhruvpahwa7602 3 жыл бұрын
How can I protect it from CSRF Attack? Please reply
@GnomePuntTrainerYT
@GnomePuntTrainerYT 4 жыл бұрын
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 4 жыл бұрын
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.
@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
@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 ?
@mimikiticat4949
@mimikiticat4949 Жыл бұрын
Thanks for the example. Does this still apply now or theres a better way.
@mydisk2859
@mydisk2859 3 жыл бұрын
How to use it with express-session and witgout nextjs api routes ?
@leighhalliday
@leighhalliday 3 жыл бұрын
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...
@siosaiafonua9286
@siosaiafonua9286 2 жыл бұрын
Which cases would you just want to use local storage?
@mohamedsba4185
@mohamedsba4185 2 жыл бұрын
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)
@hazemsaeed6372
@hazemsaeed6372 2 жыл бұрын
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.
@apollomacabodbod6329
@apollomacabodbod6329 10 ай бұрын
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?
@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?
@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.
@justfun591
@justfun591 2 жыл бұрын
After setting token from Server side, how can I use token to check whether a client is login or not ?
@uusa35
@uusa35 2 жыл бұрын
thank you
@flo_dev
@flo_dev 3 жыл бұрын
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
@satyamchourey6120
@satyamchourey6120 Жыл бұрын
Is there a size limit on server side cookies?
@manjicoder3049
@manjicoder3049 Жыл бұрын
Thanks
@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
@blackbeans3112
@blackbeans3112 10 ай бұрын
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?
@sayantankarmakar4191
@sayantankarmakar4191 4 жыл бұрын
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 4 жыл бұрын
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 4 жыл бұрын
@@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
@sushantkumar3861
@sushantkumar3861 3 жыл бұрын
I am unable to get the cookies in _app.js. How can I get it there?
@berlino5563
@berlino5563 3 жыл бұрын
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 жыл бұрын
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
@6365bharath
@6365bharath 2 жыл бұрын
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?
@Dartsbreckx
@Dartsbreckx 2 жыл бұрын
i do this , but in production i have problems
@programmingplanet3249
@programmingplanet3249 2 жыл бұрын
thank's alot
@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.
@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
@hungtranlam8419
@hungtranlam8419 2 жыл бұрын
Thank you, but can it work if I deploy nextjs app and expressjs app to different domain?
@ArunJenson-r5u
@ArunJenson-r5u Жыл бұрын
how to set the cookie in serverside in next 13 app router
@tahafurkanislek7091
@tahafurkanislek7091 Жыл бұрын
did you find the answer ?
@alexkey9372
@alexkey9372 4 жыл бұрын
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 4 жыл бұрын
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 4 жыл бұрын
@@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.
@sk-uv5fm
@sk-uv5fm 4 жыл бұрын
Great! I have a question about getServerSideProps. if router.push to the page with getServerSideProps, Is the page SSR?
@leighhalliday
@leighhalliday 4 жыл бұрын
Hey! Hmmm... I think so! I think Next.js handles that for us. I'm only 95% sure though :D
@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?
@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 жыл бұрын
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
@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 жыл бұрын
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.
@pedroapfilho
@pedroapfilho 4 жыл бұрын
What if you want to fetch data on the server with these cookies? How would it work?
@leighhalliday
@leighhalliday 4 жыл бұрын
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
@AllahomAnsorGaza
@AllahomAnsorGaza 3 жыл бұрын
how read token from _app to setUser Login
@dennist5778
@dennist5778 3 жыл бұрын
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 3 жыл бұрын
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.
@dhruvpahwa7602
@dhruvpahwa7602 3 жыл бұрын
I'm getting this error on logout: Cannot set headers after they are sent to the client
@leighhalliday
@leighhalliday 3 жыл бұрын
Hmm... double check to ensure your backend doesn't have any errors... that's something I've noticed can cause it.
@dhruvpahwa7602
@dhruvpahwa7602 3 жыл бұрын
@@leighhalliday Yeah, I was sending res.json without retuning it
@mohammaddh8655
@mohammaddh8655 2 жыл бұрын
bro you saved my ass thanks
@VikashLaBrainer
@VikashLaBrainer 2 жыл бұрын
Can anyone manipulate server side cookies.
@leighhalliday
@leighhalliday 2 жыл бұрын
If you have it as an http only cookie, nope
@Julia_Berrrlin
@Julia_Berrrlin 4 жыл бұрын
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 4 жыл бұрын
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 2 жыл бұрын
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.
Difference between cookies, session and tokens
11:53
Valentin Despa
Рет қаралды 675 М.
Jaidarman TOP / Жоғары лига-2023 / Жекпе-жек 1-ТУР / 1-топ
1:30:54
БОЙКАЛАР| bayGUYS | 27 шығарылым
28:49
bayGUYS
Рет қаралды 1,1 МЛН
Caleb Pressley Shows TSA How It’s Done
0:28
Barstool Sports
Рет қаралды 60 МЛН
Charging Money with Stripe in Next.js in 15 Minutes
18:22
Leigh Halliday
Рет қаралды 10 М.
Client-Side VS Server-Side Rendering - Data Fetching with Next.js
13:20
freeCodeCamp Talks
Рет қаралды 184 М.
Why you can't set cookies in Server Components
10:58
Ryan Toronto
Рет қаралды 11 М.
Free Next.js Course: 29 / HttpOnly Cookie Authentication
14:27
Leigh Halliday
Рет қаралды 9 М.
How to Debug Cookies When They Don't Work
11:53
Ben Awad
Рет қаралды 13 М.
How do server side authentication sessions work (express & cookies)
15:26
Jaidarman TOP / Жоғары лига-2023 / Жекпе-жек 1-ТУР / 1-топ
1:30:54