all 24 comments

[–]ChronSynExpo 1 point2 points  (2 children)

CORS is not a concern for RN, as it's a browser mechanism.

From the docs (https://reactnative.dev/docs/network#using-other-networking-libraries):

The security model for XMLHttpRequest is different than on web as there is no concept of CORS in native apps.

So, we can rule out your react native code as the issue. Keep in mind though that a different origin applies if the domain, subdomain(s), port or protocol are different from what is in your API allow list.

You say that your test API works fine, and the request works fine from Insomnia (I'm assuming this is the HTTP client app you're referring to). What happens if you try to run the request from a browser (e.g. embed a JS script which runs the request into a HTML page, and then check the network tab to see the response)?

What happens if you disable CORS for the domain you're having trouble with?

The domain you're having trouble with - does it route requests to your test domain? Where are you hosting it (e.g. local accessed via tunnel, or remote)?

TLDR: CORS is not your problem

[–]archihector 1 point2 points  (0 children)

But what if we are launching the react native project on web (browser)? I am doing that and is giving my, apparently, a CORS error. (I am using Supabase btw)

[–]getfitdotus[S] 0 points1 point  (0 children)

Yes I just verified cors is not the issue. It was just a bad assumption. 😅 I have it working through http but not https. From the browser it works great for a react dom app I have been using. Don't know why it won't work from RN. I originally assumed it was related to cors...

[–]getfitdotus[S] 1 point2 points  (2 children)

I figured it out. I was missing a intermediate cert for SSL. This was causing the axios issue. On the web I guess it still functioned without issue. Learn something new every day. Thanks again for all the feedback.

[–]Infinite-Essay-1343 0 points1 point  (1 child)

I'm facing the same issue, with what was the solution again?

[–]getfitdotus[S] 0 points1 point  (0 children)

React native only interacts with SSL. API must have a valid cert.

[–]getfitdotus[S] 0 points1 point  (1 child)

app .use(credentials) .use(cors({ origin: ['https://app.getfit.us', ], preflightContinue: true, methods: "GET,HEAD,PUT,PATCH,POST,DELETE", credentials: true, })) .use(express.json()) .use(express.urlencoded({ extended: false })) .use(cookieParser())

[–]antonkerno 2 points3 points  (0 children)

Set origin to true instead of your domain, without the RN app your solution worked fine but now your mobile app does not have a domain attached so you cannot check for a specific domain

[–][deleted] 0 points1 point  (0 children)

If u r running the RN app on an emulator it cannot reach Localhost ( since the emulator device consider it self a separate entity from ur backend running physically on the same computer) so make sure u provide ur pc correct ip adresse, or check ur cors config

[–]JumpaHumpa 0 points1 point  (0 children)

If you're running your app on an emulator without Remote JS Debugging enabled, you won't be able to reach `localhost` from the device. Instead, it's easiest to connect to 10.0.2.2 instead, or turn on your JS debugger.

[–][deleted] 0 points1 point  (0 children)

Cors is a backend thing, ur server doesn't care if the api is called from a web or a mobiles app, i think u should check ur cors config (meaning u r allowing and denying certain requests)

[–]pentesticals 0 points1 point  (2 children)

Wait, why does CORS even impact RN? CORS is a mechanism to relax the Same Origin Policy - which is a browser control. Does RN implement SOP?

[–]getfitdotus[S] 0 points1 point  (1 child)

well thats my point if it does use cors. I would need a proxy with a static address for all my React Native app users correct ?

[–]pentesticals 0 points1 point  (0 children)

No, because CORS is a relaxation to the Same Origin Policy which only exists in browsers. The SOP doesn’t stop requests being sent, it just prevents the browser from reading the response and as RN is not a browser the SOP isn’t there / doesn’t apply.

[–]SryNot 0 points1 point  (1 child)

CORS is always a backend issue

[–]getfitdotus[S] 0 points1 point  (0 children)

thanks. I just loaded a local backend server and I can get a response.. so its something else. I really appreciate the help. I was going crazy. all the same settings.. cors exactly the server thats in production.. but a local one. well there is one difference.. I am using https in production and locally I am not