async update()

in src/AuthorizeGitHub.js [21:53]


  async update() {
    let url = new URL(window.location.href);
    let code = url.searchParams.get("code");
    if (!code) {
      return;
    }
    this.state.code = code;
    let errorMsg = "bad code passed to GitHub OAuth, sign into GitHub again";
    let result = await fetch(`${AUTH_SERVER}/authenticate/${code}`)
      .then((r) => r.json())
      .catch((error) => {
        errorMsg =
          "Error happened while communicating to auth server: " + error;
        return { token: null };
      });
    if (!result.token) {
      alert(errorMsg);
    } else {
      localStorage.setItem("gh_pat", result.token);
      this.state.loggedin = true;
    }
    this.setState(this.state);

    // GitHub redirects back to a URL with just a ?code=... parameter, so store
    // off to the side the place to go once logged in
    const lastRedirect = localStorage.getItem("last_redirect");
    if (lastRedirect) {
      localStorage.removeItem("last_redirect");
      if (lastRedirect !== window.location.href) {
        window.location.href = lastRedirect;
      }
    }
  }