async function OAuthGate()

in htdocs/js/selfserve.js [95:129]


async function OAuthGate(callback) {
  const QSDict = new URLSearchParams(document.location.search);
  if (QSDict.get('action') === 'oauth') { // OAuth callback?
    const OAuthResponse = await GET(`/api/oauth?${QSDict.toString()}`);
    if (OAuthResponse.status === 200) {
      if (sessionStorageSupported()) {
        const OriginURL = window.sessionStorage.getItem('asp_origin');
        // Do we have a stored URL to redirect back to, now that OAuth worked?
        if (OriginURL) {
          window.sessionStorage.removeItem('asp_origin');
          document.location.href = OriginURL;
        }
        return;
      }
    } else {
      // Something went wrong. For now, just spit out the response as an alert.
      toast(await OAuthResponse.text());
    }
  }
  const session = await GET('/api/session');
  if (session.status === 403) { // No session set for this client yet, run the oauth process
    if (sessionStorageSupported()) {
      window.sessionStorage.setItem('asp_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}/oauth.html?action=oauth&state=${state}`);
    document.location.href = `https://oauth.apache.org/auth?redirect_uri=${OAuthURL}&state=${state}`;
  } else if (session.status === 200) { // Found a working session
    const preferences = await session.json();
    if (callback) callback(preferences, QSDict);
  } else { // Something went wrong on the backend, spit out the error msg
    toast(await session.text());
  }
}