I am making simple twitter login using passport js, but when calling twitter route, I get CORS error.
server.js
const express = require("express");
const session = require("express-session");
const dotenv = require("dotenv");
const userLoginRoutes = require("./routes/userLoginRoute");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(function (req, res, next) {
res.header(
"Access-Control-Allow-Origin",
"*"
); // update to match the domain you will make the request from
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.get("/api/auth/twitter", passport.authenticate("twitter"));
action dispatching here
import {
USER__LOGIN,
USER__LOGIN__FAILURE,
USER__LOGIN__SUCCESS,
} from "../constants/loginconstants";
const axios = require("axios");
// login action
export const userLoginAction = () => async (dispatch) => {
try {
dispatch({
type: USER__LOGIN,
});
const config = {
header: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin" : "*"
},
};
const { data } = await axios.get(`/api/auth/twitter`, config);
dispatch({
type: USER__LOGIN__SUCCESS,
payload: data,
});
} catch (error) {
dispatch({
type: USER__LOGIN__FAILURE,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
}
};
in package.json in client,
"proxy": "http://127.0.0.1:3001",
Also I cannot see Access-Control-Allow-Origin in the network tab, see here,
Is there anything wrong while setting the Access-Control-Allow-Origin
?
I cannot figure out, where am I getting wrong. I have used cors, and even manually set the origin to *.