whatadewitt / yahoo-fantasy-sports-api

NodeJS wrapper for the Yahoo! Fantasy Sports API
https://yahoo-fantasy-node-docs.vercel.app/
MIT License
200 stars 54 forks source link

CORS error on production #108

Closed Wintersboy closed 11 months ago

Wintersboy commented 11 months ago

Hello!

Has anyone dealt with a cors error while trying to authenticate a user with the yahoo sign in? I've copied the basic set up for the authentication call and replaced the key, secret, and added a redirect url. All of which are running on production. I've also set up the yahoo developer project with the same homepage and redirect url.

I can add a code example and error screenshots if necessary.

Cheers!

whatadewitt commented 11 months ago

For sure send over whatever you have! I would be happy to try to help!

Wintersboy commented 11 months ago

So this is my Yahoo auth function (no callback for now, just trying to get it working first)

image

And this is the console error I'm getting

image (where livefeed.render is my server endpoint and livefeed.netlify is my web page)

If I click the Yahoo login link that is in the console error I get taken to the login page, so my credentials must be working.

This is a small snippet of my server.js file to show that I am using the cors package which has eliminated any cors issues with other external apis.

image

I'm making a call to the /auth/yahoo endpoint in my React app.

Thanks for taking a look!

whatadewitt commented 11 months ago

Take a look at this page here: https://yahoo-fantasy-node-docs.vercel.app/auth

It looks like you have your auth and auth callback URL as the same thing, which definitely won't work. I also think you'll need to have the same server handling the request and the callback (though I can't say I've ever tried doing it with separate ones)

LMK if this helps at all?

Wintersboy commented 11 months ago

Ahhhh okay that makes sense! I'm only using the 1 server but I'll try out the callback url and let you know how it works, thanks!

Wintersboy commented 11 months ago

So I've implemented a callback using your example but it doesn't seem to be the issue. It doesn't even get to that point. The initial auth call is being blocked by cors. I've tried running my frontend on an NGROK server and I'm still getting the same cors issue with the yahoo login call. I'm really confused on how?

Any other ideas??

Wintersboy commented 11 months ago

Does the redirect URI in the yahoo developer app need to be the same as the redirect URL in the API? I wouldn't think so since ones for the frontend and the other for the backend if I'm guessing correctly?

whatadewitt commented 11 months ago

Does the redirect URI in the yahoo developer app need to be the same as the redirect URL in the API? I wouldn't think so since ones for the frontend and the other for the backend if I'm guessing correctly?

Yes, these should be the same. You're specifying the "callback", which is where Yahoo! will call when they authenticate your user.

Wintersboy commented 11 months ago

Yeah I'm still getting this error. Any chance you have a node js example I could see outside of the snippet from your docs?

whatadewitt commented 11 months ago

The entirety of the docs source (which has the whole login flow) is published here: https://github.com/whatadewitt/fantasy-docs-next -- it's in next, but a lot of the principles would be the same. There's an older, express version here: https://github.com/whatadewitt/fantasy-starter-express but I don't actually know that it works :/

If you wanted to publish the code I could clone it as well and take a look for you.

Wintersboy commented 11 months ago

I'll take a look at the Next source code and see if I can get it to work! If not, I'll clone my source code for you. Thank you I really appreciate your help!

I'll keep you posted.

On Tue, Oct 17, 2023, 11:00 a.m. Luke DeWitt @.***> wrote:

The entirety of the docs source (which has the whole login flow) is published here: https://github.com/whatadewitt/fantasy-docs-next -- it's in next, but a lot of the principles would be the same. There's an older, express version here: https://github.com/whatadewitt/fantasy-starter-express but I don't actually know that it works :/

If you wanted to publish the code I could clone it as well and take a look for you.

— Reply to this email directly, view it on GitHub https://github.com/whatadewitt/yahoo-fantasy-sports-api/issues/108#issuecomment-1766903766, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALRVQ2FRSUTCVPBB3GJIT4DX73BTRAVCNFSM6AAAAAA6DHZJNOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRWHEYDGNZWGY . You are receiving this because you authored the thread.Message ID: @.***>

Wintersboy commented 11 months ago

Ahh still no luck. Could we take this convo elsewhere and I'll give access to my repo?

whatadewitt commented 11 months ago

Might be better to carry on the convo here, just in case someone else runs into the issue? If you give me access to the repo I can clone it locally and see if I can figure out where the issue is and submit a PR with the solution?

Wintersboy commented 11 months ago

Yeah you're absolutely right! Invite sent

Wintersboy commented 11 months ago

So basically just take a look at backend/server.js and backend/util/yfAuth.js, and then I just make a simple call in frontend/src/common/TopToolbar.js that does nothing with the result atm

Wintersboy commented 11 months ago

and if it helps, this is my yahoo dev set up

image

I've tried many different redirect URIs and none work :(

whatadewitt commented 11 months ago

Cool looking app!

The first things I'm noticing:

I realize this sounds like a lot, so here's what I would do to make sure you have the flow:

Wintersboy commented 11 months ago

Okay great! Thanks so much for all the tips! I'll get working on that.

Just one question: Should the Yahoo! Dev app redirect URI be a frontend URL, or API endpoint?

Because aside from all the things you said, I'm just confused on how I'm getting the CORs error. That shouldn't be the case since I can access /auth/yahoo from anywhere but my own frontend...

whatadewitt commented 11 months ago

You need to redirect to your node backend. It will be here that you'll be able to grab/store the access and refresh tokens. Then you'll redirect the user from your node backend to your React frontend.

Also, just use cookie-session or whatever over passport for the time being. It will likely be a whole lot easier :D

Wintersboy commented 11 months ago

Ahhh okay I see! Thanks again, and I'll keep you updated :)

whatadewitt commented 11 months ago

@Wintersboy I'll have to create a proper README but I spent a couple of hours tonight and created this https://github.com/whatadewitt/fantasy-starter-node-react (I'd been meaning to have something like this for a while)

It's far from perfect but it should give you ideas of what you need to do.

Cheers!

Wintersboy commented 11 months ago

That's awesome! I really appreciate you writing that up. The link seems to be broken however.

whatadewitt commented 11 months ago

https://github.com/whatadewitt/fantasy-starter-node-react

Weird... I may have made it private by mistake. Try again in a few mins if that doesn't work...

whatadewitt commented 11 months ago

Confirming, it was set to private. Should be good now.

Wintersboy commented 11 months ago

Yup it's good now! It looks like a great start and should help tons!

whatadewitt commented 11 months ago

closing for now -- if you have issues feel free to open a new ticket or do it on the new repo

FernandoTorre commented 1 month ago

@Wintersboy I'm curious, did you ever resolve your issue? I'm having the same issue and I'm also kind of stumped.

Wintersboy commented 1 month ago

@Wintersboy I'm curious, did you ever resolve your issue? I'm having the same issue and I'm also kind of stumped.

Hey man! Yeah I did. The issue for me was that I was trying to make a fetch request to the auth endpoint when I just needed to use an href on my button. I assume this is because the auth endpoint has a redirect? I'm not sure why this worked but I had no issues afterwards.

FernandoTorre commented 1 month ago

I appreciate the quick reply! That's diffferent from my situation, but I'll keep it in mind as I explore other options.

FernandoTorre commented 1 month ago

Just in case anybody runs into something similar, my own issue was caused by trying to redirect from inside an AJAX call, so CORS was not the actual culprit in my case.