@BarryCarlyon Sir I have tried everything but nothing works.
All I want to do is that I have an express server running at port 3001 and react client running at 3000
My Registered URI: http://localhost:3001/auth/twitch/callback
I want to get the oAuth access_token
. On the client side first I send the user to registered redirect URI as follows:
const twitchAuth = axios.create({ baseURL: "http://localhost:3001/auth/twitch/callback", });
On the server side,
OAuth2Strategy.prototype.userProfile = function (accessToken, done) {
const options = {
url: “https://api.twitch.tv/helix/users”,
method: “GET”,
headers: {
“Client-ID”: TWITCH_CLIENT_ID,
Accept: “application/vnd.twitchtv.v5+json”,
Authorization: "Bearer " + accessToken,
},
};
request(options, function (error, response, body) {
if (response && response.statusCode == 200) {
done(null, JSON.parse(body));
} else {
done(JSON.parse(body));
}
});
};
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
passport.use(
“twitch”,
new OAuth2Strategy(
{
authorizationURL: “https://id.twitch.tv/oauth2/authorize”,
tokenURL: “https://id.twitch.tv/oauth2/token”,
clientID: TWITCH_CLIENT_ID,
clientSecret: TWITCH_SECRET,
callbackURL: CALLBACK_URL,
state: true,
},
function (accessToken, refreshToken, profile, done) {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
done(null, profile);
}
)
);
// Set route to start OAuth link, this is where you define scopes to request
app.get(
“/auth/twitch”,
passport.authenticate(“twitch”, { scope: “user:read:email” })
);
// Set route for OAuth redirect
app.get(
“/auth/twitch/callback”,
(req, res, next) => {
console.log(“hit to callback”);
next();
},
passport.authenticate(“twitch”, {
successRedirect: “/”,
failureRedirect: “/”,
})
);
I have tried setting up header as well as tried using the proxy server but still the CORS error occurs.
Error: Access to XMLHttpRequest at 'https://www.twitch.tv/login?client_id=myclientId&edirect_ur=http://localhost:3001auth/twitch/callback&response_type=code&state%3DQBENkeXou6FJKTSntHiK7H1z' (redirected from 'http://localhost:3001/auth/twitch/callback') from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have spent way too much time on this. I don’t know what am I missing. Kindly help