Fetch, CORS, and Cookies

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 พ.ค. 2020
  • This tutorial explains the process of setting cookies in the browser as well as from the server. It shows the process of setting cookies from the server using NodeJS and Express.
    The various situations that can arise with cookies when trying to set them via fetch and how CORS scenarios will impact those requests and the cookies.
    Code repo from the video - github.com/prof3ssorSt3v3/fet...

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

  • @amish198
    @amish198 5 หลายเดือนก่อน +1

    Sir your teaching is really great, I have seen lots of videos about this topic on yt but no one can teach you like this ,you are awesome
    Thank you so much from India

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

    Simply superb. The way you reveal the intracies in such a clear, progressive way is fantastic. I finally understand CORS for the first time. Thank you so much.

  • @Daniel-nb3kk
    @Daniel-nb3kk 3 ปีที่แล้ว +1

    Great job, Steve! Explained a complex topic very concisely and professionally! :)

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

    This is excellent. A very difficult topic. Thanks for the hard work that went into this.

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

    Seriously great video. I've been trying to figure all this out for days and this video was 100% exactly what I needed, really awesome stuff.

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

    You’re truly a life saver! I’ve been searching for a video like this everywhere on the internet!!!

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

    Such a clear explanation of everything! Loved it.

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

    Oh my god. You made this SO clear, thank you so much, I was breaking my head around this concept, trying to set things up with AWS. Many MANY thanks, from France, best regards !

  • @ivan.romaniv
    @ivan.romaniv 2 ปีที่แล้ว

    That's the best CORS explanation ever!! thank you!!!

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

    I was suffering with cookies and fetch until now. Thank you so much

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

    This channel never disappoints with a true sting

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

    Saved tons of time! Thanks🙏

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

    Super insightful, prof! I really appreciate what you have done for me in my career as an engineer! You have really my skills in a way I could only have dreamed of! Blessings!

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

      I know I am kinda randomly asking but do anyone know a good place to watch newly released series online?

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

      @Karsyn Mayson Flixportal =)

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

      @Luis Edward thank you, signed up and it seems to work :D Appreciate it !!

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

      @Karsyn Mayson you are welcome =)

  • @abulfazlhaidary2002
    @abulfazlhaidary2002 4 ปีที่แล้ว

    Excellent job, thank you so much.

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

    Great explanations. Thanks.

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

    easy to understand with you, thank you

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

    You deserve 1 million likes, thanks a lot !

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

    Absolute great content ! THX :)

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

    Excellent video.

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

    I can't express my appreciation

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

    You were right, I keep coming back to this video haha

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

    That was very helpful, thank you

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

    Wow. This is absolute gold! Thank you so much for doing this!
    There are so many things to this that I'm going to have to watch this a few times, but that's not your fault. Your explanation is really good. I will take some time to wrap my head around this haha

  • @84Juli1
    @84Juli1 2 ปีที่แล้ว

    Thank you !!

  • @IonutBucur-o_0
    @IonutBucur-o_0 4 ปีที่แล้ว

    Thank you!

  • @user-bn4vp2od9s
    @user-bn4vp2od9s 3 ปีที่แล้ว

    You cool bro. From Belarus with brother's love

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

    Cannot thank you enough. Going through tens of SO posts, but seeing everything in one place like this helped click it all together.

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

    my dude steve is the bob ross of programming

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

    I was actually looking for the cookies content. Was stuck in a MERN project and can't able to access the backend cookies 🍪 on to the frontend.
    Thanks for the video.

  • @lucas.n
    @lucas.n 4 ปีที่แล้ว

    damn this shit is a whole lot more convoluted than i ever thought haha And I've been doing web things since 2000s.

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

    Thanks Steve for the video. Do you have video for passing Authorization in header for CORS

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว

      Depends on what kind of authorization you are talking about.
      JWT - th-cam.com/video/QCCmWNlEkdY/w-d-xo.html
      headers in fetch responses - th-cam.com/video/5gnz6NZ3TRA/w-d-xo.html
      headers in requests - th-cam.com/video/YJ7ZgGnhN5k/w-d-xo.html
      access control allow origin header (using apache) th-cam.com/video/zswi0cPMxsU/w-d-xo.html

  • @dhgit3914
    @dhgit3914 4 ปีที่แล้ว

    great 10x :-)

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

    Does cors enabled server treat the domain name as cross origin or same origin? I mean the host ip could be considered as 127... and localhost as domain name?

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

    can you please help me
    im writing a server that sends session cookies (using express session), and a frontend website that makes request to it. It worked fine locally when i set up both the server and the website on different ports and also got the warning sameSite=lax when changed it to localhost. But when i deployed the server and the website, I changed to sameSite to none (as it will be cross site request) and set secure to true but even then I'm not receiving cookies from my response. pls help
    UPDATE: I tried setting samesite to lax and deployed it again, i got the same warning "samesite=lax should be set to none" and when i set to none, i get no cookie back :(

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

    Hi Steve, I am still a little lost. How can someone use a JWT with cors then? Where else can I store it, if JS cannot access set-cookies?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 ปีที่แล้ว

      Your JWT token can be saved in cookies or localstorage or indexeddb. JS can set cookies but only for the same domain as the html file that loaded the JS.

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

    really great video. Was using req.origin as the "allow-origin" value just for demo? For CSRF protection in production, is that ok? I can't find a proper way to use auth jwt http-only in a public b2c style app because http-only requires credentials: true which requires known origins which for an publicly open app we won't know.. Thanks so much.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว +1

      It will depend on the site / api that you are building what you use as the allow origin origin. Do you want to allow requests from anywhere? If so, then req.origin is fine. If not then you will be looking at other values like headers or query string values with an api key. the http-only cookies can be used for navigation type requests made by the browser but will never be accessible to any client-side script driven fetching.

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

      @@SteveGriffith-Prof3ssorSt3v3 I get confused then with the client side (public user's computer), that's interacting with a front end client side framework like React. Since it's CSR, is the client ,some computer on the internet, going to be always making a CORS call because client's computer will obviously be on a different domain, than the host of the React server and backend server. If that's true then to use http-only, i would always need to use req.origin as the allowed-origin?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว

      @LeftThumbBreak CORS means that the main origin is the one where the HTML file came from. Any other files loaded from that point on will have their origin compared to the html one

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

    hello I need help, I have links hls live streaming on these links I have to enable the cors support, because now the view of the live channels, after a minute is blocked, for this I have to enable sharing between the origins can you help me? Taking into account that I cannot access the server so I should bypass the cors, can you help me?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว

      CORS is security implemented by the browser to protect users. You need both the server and the browser to agree that it is safe for users to access content. It is not something that you bypass.

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

    Hi good explanation Brother although my problem has not been fixed, I use react with laravel sanctum for jwt token I think there is a problem with how they handle cors or the axios does not work propery

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 ปีที่แล้ว

      I've never used Laravel sanctum. I assume that there is something that you need to do in order to send the proper headers from the server. Without the access-control-allow-origin header you will get CORS errors.

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

    Great video! Though I've been having issues setting my cookie in the browser when I have my server deployed on Heroku and my frontend deployed on Netlify. It works on localhost but not in production. Do you happen to have any idea why the cookie is not being set? The cookie is being passed to the response in the Set-Cookie header, but for some reason it is not being set in the response.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว

      Cookies are tied to the domain that they come from. If a cookie comes from server A then server B is not allowed to see it. That means files from server B cannot read it.

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

      @@SteveGriffith-Prof3ssorSt3v3 That makes sense. So is there an alternative to cookies that I can use? I could probably set the cookie from the front end, but I don't know if that's a good idea.

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว

      @@jorgegabitto1813 there are many ways to pass data between client and server or save data locally. It all depends on what you are looking to achieve with the data.

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

      @@SteveGriffith-Prof3ssorSt3v3 Essentially I want to make sure that the user is logged in. The cookie has a token that the user receives after they log in. What are my options for saving that token?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว

      @@jorgegabitto1813 cookies or localstorage or sessionStorage are your options for saving tokens

  • @PraexorVS
    @PraexorVS 4 ปีที่แล้ว

    But fetch and cors are the same? I use fetch to bring data from an public api to my website without cors

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 ปีที่แล้ว +2

      fetch is the API used to make requests for remote data. CORS (cross-origin resource sharing) is the set of rules governing how browsers must handle data that comes from domains that are different than the one the HTML came from. They are intimately connected but they are different things.
      The CORS rules apply to every cross-origin request. If your public api requests are working it means that the browser and the server are meeting all the CORS requirements.

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

    Thank you. But you did not explain about fetching from a mobile device for example from an iphone. cross site cookies wont be sent. Can you please talk about this?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  4 หลายเดือนก่อน

      That is just the nature of cookies and the changes over the last few years. 3rd-party cookies are being blocked more and more, not just on mobile.

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

    Hey Steve, is this video still up-to-date? I'm working on a project with a very similar scenario... everything was working while on localhost, I read the mozilla documentations and had implemented everything as you have done in this video. However, when deploying it to a remote server and accessing it over https, the browser wont set the cookies from a cors request anymore, not even with a "Secure" attribute set. Now I am reading some comments on stack overflow, stating that browsers "generally refuse to set such third party cookies", which is in complete contradiction to your video and to the explanations on the mozilla doc... what is your opinion to all of this?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว

      cookies are only meant to be set by your script for the origin/domain that loaded your script. If you are talking about CORS requests then you are, by definition, talking with a different origin/domain. You can control whether your current cookies are sent with the requests to those other origins but you cannot set them.

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

      @@SteveGriffith-Prof3ssorSt3v3 Ok thank you! This means, if for example I am doing a CORS fetch request from domain A to domain B, cookies for domain B could be sent along with the request, if they had been set before by a script of domain B itself, correct? but the browser would ignore any "Set-Cookie" headers for domain B in the response of the CORS request?

  • @Ofcl-Javed
    @Ofcl-Javed ปีที่แล้ว

    A great video indeed sir, Please help me out here, I have created a rest API using node and express which i hosted at localhost:8080 and it working just fine with postman. Now i created a separate frontend project at localhost:5500 with vanilla js not any framework so i get the CORS error when fetching the api then i updated by backend use the cors and set the desired option and when i sent a request from port 5500 to port 8080 it set the cookie in res headers i mean api send back the token in res but the cookie get set at the api endpoint like i made a fetch a request from localhost:5500 to localhost:8080 and in response i get the json messge but the cookie has been set at the origin localhost:8080 , although i'm still able to access the cookie but why it set on the backend endpoint please help

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว

      When cookies are set, they are connected to the origin that set them. The server can only set them for that origin. The script in the browser can only set them for the origin that loaded the HTML file.

  • @naive-fleek7420
    @naive-fleek7420 2 ปีที่แล้ว +1

    saved my ass bro

  • @b.o.t7888
    @b.o.t7888 3 ปีที่แล้ว

    Doesn't work anymore, right? The warning also said the same around 30 mins in video

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

    What about set in browser a httpOnly cookie.?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว +2

      That will stop any JavaScript from using the cookies and their values. That works if you don't need the access.

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

    Can somebody segment this video?

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

    Was it just me or you guys also noticed that there is an Om 🕉 sound in the background after 15:30 .

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

    Thank you so much for your efforts 🌹🫶🫶 i had one problem that the Cookie Request header will not be sent in Firefox 106.0.5 64bit on ubuntu i will test. it on windows and leave a comment.
    i think this because 3rd party cookie are blocked by default in firefox