Cross Origin Request Not Sending Token

Hey, so i’m in desperate need of some assistance. I’m attempting to migrate an existing frontend site into wix. To give you a brief overview of the structure basically there is an API running on a hosted server connected to a database. The public frontend site allows users to login and retrieve personalized information from the database. This is all working fine and this structure cant be changed.

The previous developer used JSONP and ignored all the fun CORS issues. Because i’m trying to do this in wix, i have had to use the Fetch API and its not been going too well. I was hoping someone could have a look at the code i’m using for my calls and point me in the right direction.

Currently the authentication call works perfectly, a token is returned and all is good. The issue i have is it refuses to send it with the next request. All tests with postmaster and even direct browser calls work fine.


/**

  • Authentication call, sends username and password to backend and returns a token
    */
    function authenticate(login, password) {
    return fetch($apiurl + “login?username=” + login + “&password=” + password, {
    method: ‘POST’,
    headers: {
    ‘Content-Type’: ‘application/json; charset=utf-8’,
    ‘Accept’: ‘application/json’,
    },
    credentials: ‘include’,
    mode: ‘cors’

    }).then( function (response) {
    return response.json();
    })
    }


/**

  • Sends token and returns personalized user data.
    */
    function getUserDetails() {
    return fetch($apiurl + “userdetails”, {
    headers: {
    ‘Content-Type’: ‘application/json; charset=utf-8’,
    ‘Accept’: ‘application/json’,
    },
    credentials: ‘include’,
    mode: ‘cors’
    }).then( function (response) {
    return response.json();
    })
    }

Thanks in advance for any insight you can give me. I’m honestly at my wits end with this one.

Without knowing the details of the API it’s very tough to know what’s happening, not happening, or what’s supposed to happen.

Maybe some examples can help. Take a look at the Advanced examples - many of them access 3rd party services.

I don’t believe this to be an issue with the backends API as it has been working in the past. Using jquery i’m able to make these calls without issue. I think its something to do specifically with fetch or how i’m implementing the call in fetch.

Basically there is a server hosting the api e.g backend.mydomain.com, it uses oAuth, so the wix page makes a call with a username and password. The api compares that with the database of user accounts and if everything matches returns a token to be used for future authenticated calls.

That call works, i can check my tokens after the call as see that a token was set for backend.mydomain.com. The part of this that i cant seem to solve is how to get fetch to resend the token on its next request?