src/interactive-auth.js
function(auth, background) {
        const self = this;

        // hackery to make sure that synchronous exceptions end up in the catch
        // handler (without the additional event loop entailed by q.fcall or an
        // extra Promise.resolve().then)
        let prom;
        try {
            prom = this._requestCallback(auth, background);
        } catch (e) {
            prom = Promise.reject(e);
        }

        prom = prom.then(
            function(result) {
                console.log("result from request: ", result);
                self._completionDeferred.resolve(result);
            }, function(error) {
                // sometimes UI auth errors don't come with flows
                const errorFlows = error.data ? error.data.flows : null;
                const haveFlows = Boolean(self._data.flows) || Boolean(errorFlows);
                if (error.httpStatus !== 401 || !error.data || !haveFlows) {
                    // doesn't look like an interactive-auth failure. fail the whole lot.
                    throw error;
                }
                // if the error didn't come with flows, completed flows or session ID,
                // copy over the ones we have. Synapse sometimes sends responses without
                // any UI auth data (eg. when polling for email validation, if the email
                // has not yet been validated). This appears to be a Synapse bug, which
                // we workaround here.
                if (!error.data.flows && !error.data.completed && !error.data.session) {
                    error.data.flows = self._data.flows;
                    error.data.completed = self._data.completed;
                    error.data.session = self._data.session;
                }
                self._data = error.data;
                self._startNextAuthStage();
            },
        );
        if (!background) {
            prom = prom.catch((e) => {
                this._completionDeferred.reject(e);
            });
        } else {
            // We ignore all failures here (even non-UI auth related ones)
            // since we don't want to suddenly fail if the internet connection
            // had a blip whilst we were polling
            prom = prom.catch((error) => {
                console.log("Ignoring error from UI auth: " + error);
            });
        }
        prom.done();
    }
function(auth, background) {
        const self = this;

        // hackery to make sure that synchronous exceptions end up in the catch
        // handler (without the additional event loop entailed by q.fcall or an
        // extra Promise.resolve().then)
        let prom;
        try {
            prom = this._requestCallback(auth, background);
        } catch (e) {
            prom = Promise.reject(e);
        }

        prom = prom.then(
            function(result) {
                console.log("result from request: ", result);
                self._completionDeferred.resolve(result);
            }, function(error) {
                // sometimes UI auth errors don't come with flows
                const errorFlows = error.data ? error.data.flows : null;
                const haveFlows = Boolean(self._data.flows) || Boolean(errorFlows);
                if (error.httpStatus !== 401 || !error.data || !haveFlows) {
                    // doesn't look like an interactive-auth failure. fail the whole lot.
                    throw error;
                }
                // if the error didn't come with flows, completed flows or session ID,
                // copy over the ones we have. Synapse sometimes sends responses without
                // any UI auth data (eg. when polling for email validation, if the email
                // has not yet been validated). This appears to be a Synapse bug, which
                // we workaround here.
                if (!error.data.flows && !error.data.completed && !error.data.session) {
                    error.data.flows = self._data.flows;
                    error.data.completed = self._data.completed;
                    error.data.session = self._data.session;
                }
                self._data = error.data;
                self._startNextAuthStage();
            },
        );
        if (!background) {
            prom = prom.catch((e) => {
                this._completionDeferred.reject(e);
            });
        } else {
            // We ignore all failures here (even non-UI auth related ones)
            // since we don't want to suddenly fail if the internet connection
            // had a blip whilst we were polling
            prom = prom.catch((error) => {
                console.log("Ignoring error from UI auth: " + error);
            });
        }
        prom.done();
    }
function ( auth , background ) { const self = this ; // hackery to make sure that synchronous exceptions end up in the catch // handler (without the additional event loop entailed by q.fcall or an // extra Promise.resolve().then) let prom ; try { prom = this . _requestCallback ( auth , background ) ; } catch ( e ) { prom = Promise . reject ( e ) ; } prom = prom . then ( function ( result ) { console . log ( "result from request: " , result ) ; self . _completionDeferred . resolve ( result ) ; } , function ( error ) { // sometimes UI auth errors don't come with flows const errorFlows = error . data ? error . data . flows : null ; const haveFlows = Boolean ( self . _data . flows ) || Boolean ( errorFlows ) ; if ( error . httpStatus !== 401 || ! error . data || ! haveFlows ) { // doesn't look like an interactive-auth failure. fail the whole lot. throw error ; } // if the error didn't come with flows, completed flows or session ID, // copy over the ones we have. Synapse sometimes sends responses without // any UI auth data (eg. when polling for email validation, if the email // has not yet been validated). This appears to be a Synapse bug, which // we workaround here. if ( ! error . data . flows && ! error . data . completed && ! error . data . session ) { error . data . flows = self . _data . flows ; error . data . completed = self . _data . completed ; error . data . session = self . _data . session ; } self . _data = error . data ; self . _startNextAuthStage ( ) ; } , ) ; if ( ! background ) { prom = prom . catch ( ( e ) => { this . _completionDeferred . reject ( e ) ; } ) ; } else { // We ignore all failures here (even non-UI auth related ones) // since we don't want to suddenly fail if the internet connection // had a blip whilst we were polling prom = prom . catch ( ( error ) => { console . log ( "Ignoring error from UI auth: " + error ) ; } ) ; } prom . done ( ) ; }
Fire off a request, and either resolve the promise, or call
startAuthStage.

@private
@param {object?} auth new auth dict, including session id
@param {bool?} background If true, this request is a background poll, so it
failing will not result in the attemptAuth promise being rejected.
This can be set to true for requests that just poll to see if auth has
been completed elsewhere.
Fire off a request and either resolve the promise or call startAuthStage .