Wait for the CORS cookie to be set before the location.href redirect

I am trying to make a login that persists across multiple domains (which I control). My strategy, after a successful login response, I send a CORS request to get a cookie for each of the other required domains. When cookies are returned, I am redirected to the users home page (which can be on any of the domains)

I find that everything works if I comment out the redirect: all CORS files are installed and I am logged into other domains. However, when I redirect, the CORS cookie is sometimes not set. Here is the general idea of ​​my client side code:

    // This is running client side when the user visits "A.com/index.html"
    // PLEASE NOTE: the distinction between A.com and B.com is important

     $.post('https://A.com/attemptLogin', function(data) {
        if (!data.success) { return; }
        var token = data.loginToken;
        var userURL = data.userURL; // userURL may be in A.com or B.com...
        // now get a cookie at the alternate domain B.com
        $.ajax({
            url: 'http://B.com/getDomainCookie/' + token,
            method: 'GET',
            xhrFields: { withCredentials: true },
            success: function() {
                // if I comment out the next line, the cookie is always set successfully
                // if I leave the line in, it is hit or miss whether the cookie is set.
                location.href = userURL;
            }
        });
    });

      

I am assuming that the ajax callback is triggered before the browser finishes setting the cookie, possibly related to the fact that it is a B.com cookie, and this code is running on A.com.

Any help is appreciated. This is my first question, so constructive criticism on ettiquette and formatting is also welcome!

+3


source to share





All Articles