I’m building a Node app that uses http and socket, and i’m exposing my localhost using Ngrok and letting other user connect to my react app using my IP4 Address.
i always get this error :
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ‘https://a29a-41-109-30-183.ngrok-free.app/socket.io/?EIO=4&transport=polling&t=O-hTmol’. (Reason: Credential is not supported if the CORS header ‘Access-Control-Allow-Origin’ is ‘*’).
I setup the CORS like this :
app.use(
cors({
origin: ["http://localhost:5173", "http://192.168.1.6:5173"],
credentials: true,
})
);
const server = http.createServer(app);
const io = new SocketIOServer(server, {
cors: {
origin: ["http://localhost:5173", "http://192.168.1.6:5173"],
credentials: true,
},
});
and on the client side :
const socket = io(backend_url_socket, {
withCredentials: true,
extraHeaders: {
Authorization: `Bearer ${secureLocalStorage.getItem("authToken")}`,
"Another-Header": "HeaderValue",
},
});
socket.on("connect", () => {});