async function OAuthGate()

in static/plugins/zzz_primer.js [44:83]


async function OAuthGate(callback, oauth_optional=false) {
    const QSDict = new URLSearchParams(document.location.search);
    if (QSDict.get('action') === 'oauth') { // OAuth callback?
        const OAuthResponse = await fetch(`/api/oauth?${QSDict.toString()}`);
        if (OAuthResponse.status === 200) {
            if (sessionStorageSupported()) {
                const OriginURL = window.sessionStorage.getItem('ird_origin');
                // Do we have a stored URL to redirect back to, now that OAuth worked?
                if (OriginURL) {
                    window.sessionStorage.removeItem('ird_origin');
                    document.location.href = OriginURL;
                }
                return;
            }
        } else {
            // Something went wrong. For now, just spit out the response as an alert.
            alert(await OAuthResponse.text());
        }
    }
    if (oauth_optional) return
    const session = await fetch('/api/session');
    if (session.status === 403) { // No session set for this client yet, run the oauth process
        if (sessionStorageSupported()) {
            window.sessionStorage.setItem('ird_origin', document.location.href); // Store where we came from
        }
        // Construct OAuth URL and redirect to it
        const state = uuid();
        const OAuthURL = encodeURIComponent(`https://${document.location.hostname}/?action=oauth&state=${state}`);
        document.location.href = `https://oauth.apache.org/oauth-oidc?redirect_uri=${OAuthURL}&state=${state}`;
    } else if (session.status === 200) { // Found a working session
        const preferences = await session.json();
        const ue = document.getElementById('useremail');
        const um = document.getElementById('usermenu');
        if (ue) ue.innerText = `${preferences.uid}@apache.org`;
        if (um) um.style.display = "block";
        if (callback) callback(preferences, QSDict);
    } else { // Something went wrong on the backend, spit out the error msg
        alert(await session.text());
    }
}