async ensureAccessToken(context, userContext) {
        if (!userContext) {
            userContext = await this.getUserContext(context);
        }
        if (!userContext) {
            throw new Error(`Cannot return an access token because the user is not authenticated.`);
        }
        if (userContext.status !== 'authenticated') {
            throw new Error(
                `Cannot return an access token because the user is not authenticated. Current status is '${userContext.status}'`
            );
        }
        if (userContext.vendorToken) {
            if (userContext.vendorToken.access_token && userContext.vendorToken.expires_at > Date.now() + 30000) {
                return userContext.vendorToken.access_token;
            }
            if (userContext.vendorToken.refresh_token) {
                userContext.status = 'refreshing';
                try {
                    await this.saveUserContext(userContext);
                    userContext.vendorToken = await this.refreshAccessToken(userContext.vendorToken);
                    if (!isNaN(userContext.vendorToken.expires_in)) {
                        userContext.vendorToken.expires_at = Date.now() + +userContext.vendorToken.expires_in * 1000;
                    }
                    userContext.vendorUserProfile = await this.getUserProfile(userContext.vendorToken);
                    userContext.status = 'authenticated';
                    await this.saveUserContext(userContext);
                    return userContext.vendorToken.access_token;
                } catch (e) {
                    await this.deleteVendorUser(context.fusebit, userContext);
                    Sdk.debug('REFRESH TOKEN ERROR', e);
                }
            }
        }
        throw new Error('User logged out. Unable to obtain an access token.');
    }
/**
     * Returns a valid access token to the vendor's system representing the vendor's user described by the userContext.
     * If the currently stored access token is expired or nearing expiry, and a refresh token is available, a new access
     * token is obtained, stored for future use, and returned. If a current access token cannot be returned, an exception is thrown.
     * @param {TurnContext} context The turn context
     * @param {*} userContext The user context representing the vendor's user. Contains vendorToken and vendorUserProfile, representing responses
     * from getAccessToken and getUserProfile, respectively. This is optional and if not provided, will be obtained from context.
     */
Returns a valid access token to the vendor's system representing the vendor's user described by the userContext.
If the currently stored access token is expired or nearing expiry, and a refresh token is available, a new access
token is obtained, stored for future use, and returned. If a current access token cannot be returned, an exception is thrown.
@param {TurnContext} context The turn context
@param {*} userContext The user context representing the vendor's user. Contains vendorToken and vendorUserProfile, representing responses
from getAccessToken and getUserProfile, respectively. This is optional and if not provided, will be obtained from context.
Returns a valid access token to the vendor ' s system representing the vendor ' s user described by the userContext . If the currently stored access token is expired or nearing expiry and a refresh token is available a new access token is obtained stored for future use and returned . If a current access token cannot be returned an exception is thrown . @param { TurnContext } context The turn context @param { * } userContext The user context representing the vendor ' s user . Contains vendorToken and vendorUserProfile representing responses from getAccessToken and getUserProfile respectively . This is optional and if not provided will be obtained from context .
async ensureAccessToken(context, userContext) {
        if (!userContext) {
            userContext = await this.getUserContext(context);
        }
        if (!userContext) {
            throw new Error(`Cannot return an access token because the user is not authenticated.`);
        }
        if (userContext.status !== 'authenticated') {
            throw new Error(
                `Cannot return an access token because the user is not authenticated. Current status is '${userContext.status}'`
            );
        }
        if (userContext.vendorToken) {
            if (userContext.vendorToken.access_token && userContext.vendorToken.expires_at > Date.now() + 30000) {
                return userContext.vendorToken.access_token;
            }
            if (userContext.vendorToken.refresh_token) {
                userContext.status = 'refreshing';
                try {
                    await this.saveUserContext(userContext);
                    userContext.vendorToken = await this.refreshAccessToken(userContext.vendorToken);
                    if (!isNaN(userContext.vendorToken.expires_in)) {
                        userContext.vendorToken.expires_at = Date.now() + +userContext.vendorToken.expires_in * 1000;
                    }
                    userContext.vendorUserProfile = await this.getUserProfile(userContext.vendorToken);
                    userContext.status = 'authenticated';
                    await this.saveUserContext(userContext);
                    return userContext.vendorToken.access_token;
                } catch (e) {
                    await this.deleteVendorUser(context.fusebit, userContext);
                    Sdk.debug('REFRESH TOKEN ERROR', e);
                }
            }
        }
        throw new Error('User logged out. Unable to obtain an access token.');
    }
async ensureAccessToken ( context , userContext ) { if ( ! userContext ) { userContext = await this . getUserContext ( context ) ; } if ( ! userContext ) { throw new Error ( ` ` ) ; } if ( userContext . status !== 'authenticated' ) { throw new Error ( ` ${ userContext . status } ` ) ; } if ( userContext . vendorToken ) { if ( userContext . vendorToken . access_token && userContext . vendorToken . expires_at > Date . now ( ) + 30000 ) { return userContext . vendorToken . access_token ; } if ( userContext . vendorToken . refresh_token ) { userContext . status = 'refreshing' ; try { await this . saveUserContext ( userContext ) ; userContext . vendorToken = await this . refreshAccessToken ( userContext . vendorToken ) ; if ( ! isNaN ( userContext . vendorToken . expires_in ) ) { userContext . vendorToken . expires_at = Date . now ( ) + + userContext . vendorToken . expires_in * 1000 ; } userContext . vendorUserProfile = await this . getUserProfile ( userContext . vendorToken ) ; userContext . status = 'authenticated' ; await this . saveUserContext ( userContext ) ; return userContext . vendorToken . access_token ; } catch ( e ) { await this . deleteVendorUser ( context . fusebit , userContext ) ; Sdk . debug ( 'REFRESH TOKEN ERROR' , e ) ; } } } throw new Error ( 'User logged out. Unable to obtain an access token.' ) ; }
Returns a valid access token to the vendor's system representing the vendor's user described by the userContext.
Returns a valid access token to the vendor ' s system representing the vendor ' s user described by the userContext .