Axios CORS issue with Github oauth Not getting access token

I have created 2 routes on my React-Redux app. I have added github applications settings with homepage and callback URL already.

When you hit this route :
You click on Github login button, ==> githubGeturi

Github redirects back with a code
and githubSendCode(‘9536286a59228e7784a1’) action is triggered

You can see in network call OPTIONS call goes through, but POST call never happens. and you get a console error:

XMLHttpRequest cannot load…_secret=123456789123456789123456789&code=9536286a59228e7784a1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.

Below is my action functions:

const CLIENT_ID = '32b70bf671e04762b26c';
const CLIENT_SECRET = '123456789123456789123456789';
const ROOT_URL = window.location.origin;
const REDIRECT_URL = `${ROOT_URL}/auth/callback`;
const AUTHORIZE_URL = '';
const ACCESS_TOKEN_URL = '';
const STATE = _.random(10000);

export function githubGeturi() {
  const GITHUB_URL = `${AUTHORIZE_URL}?client_id=${CLIENT_ID}&scope=user,public_repo&redirect_uri=${REDIRECT_URL}`;

  return (dispatch) => dispatch(signinUrl(GITHUB_URL));

export function githubSendCode(code) {
  const GITHUB_URL = `${ACCESS_TOKEN_URL}?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&code=${code}`;['Access-Control-Allow-Origin'] = '*';
  const axiosPost =
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'text/json'

  return (dispatch) => {
    return axiosPost
        success => dispatch(signinSuccess(success)),
        error => dispatch(signinError(error))

The only possible way I found is make POST call with server.
You can view the entire solution here:

It seems like you can’t make a call to that end point via JavaScript

On your example I see that OPTIONS method call fails, and this is because axios does that when you add extra headers to request, but POST call fails as well.

You can setup a proxy in between your app and github on your server which simply forwards your requests and replies with response.

Read More:   How do I clear all options in a dropdown box?

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts