Matt Fowler Matt Fowler -4 years ago 110
React JSX Question

Oauth Redirect Returning No 'Access-Control-Allow-Origin' header is present on the requested resource

I'm currently implementing OAuth login with LinkedIn in my React and Play app and am running into a CORS error when trying to redirect to the authorization page in my dev environment:

XMLHttpRequest cannot load…basicprofile&redirect_uri=http%3A%2F%2Flocalhost%3A9000%2Fusers%2Flinkedin. Redirect from '…basicprofile&redirect_uri=http%3A%2F%2Flocalhost%3A9000%2Fusers%2Flinkedin' to '…' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I have the following setup:

  • Play server running at localhost:9000

  • React app (created via create-react-app) running at localhost:3000

My JS code calls the
endpoint which is implemented as follows:

Action { implicit req: RequestHeader =>
val csrfToken = CSRF.getToken.get.value
Redirect(linkedinUrl(oauthConfig.linkedinClientId, csrfToken)).withSession("state" -> csrfToken)

I have my Play application set to handle CORS appropriately.

My react app just makes a request to the above endpoint via Axios:


This responds with a 303 with a redirect to the LinkedIn auth page which then gives me the error.

How do I get the CORS policy working correctly in this dev setup? I've tried adding the following to my package.json as the create-react-app documentation recommends:

"proxy": "http://localhost:9000",

And I've also tried setting a request header to
"Access-Control-Allow-Origin" : "*"
on the redirect in the Play server with no success.

Note that going to localhost:9000/auth/linkedin redirects properly.

Answer Source responses apparently don’t include the Access-Control-Allow-Origin response header, and because they do not, your browser blocks your frontend JavaScript code from accessing the responses.

There are no changes you can make to your own frontend JavaScript code nor backend config settings that’ll allow your frontend JavaScript code to make requests the way you’re trying directly to and get responses back successfully. explains in more detail but the gist of it is that for CORS, the server the request is being sent to must be configured to send the Access-Control-Allow-Origin response header, nor your own backend server.

Anyway has official docs that explain how to request authorization for a user cross-origin, which appears to be just this:

<script type="text/javascript" src="//">
    api_key:   [API_KEY]
    onLoad:    [ONLOAD]
    authorize: [AUTHORIZE]
    lang:      [LANG_LOCALE]

IN.User.authorize(callbackFunction, callbackScope);

And has other docs that another auth flow:

<script type="in/Login"></script> <!-- Create the "Sign In with LinkedIn" button-->

<!-- Handle async authentication & retrieve basic member data -->
<script type="text/javascript">

    // Setup an event listener to make an API call once auth is complete
    function onLinkedInLoad() {
        IN.Event.on(IN, "auth", getProfileData);

    // Handle the successful return from the API call
    function onSuccess(data) {

    // Handle an error response from the API call
    function onError(error) {

    // Use the API call wrapper to request the member's basic profile data
    function getProfileData() {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download