File size: 1,454 Bytes
013ff92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f97221f
013ff92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useEffect, useRef } from "react";
import { useNavigate } from "react-router-dom";
import { authApi, safeNext } from "../api/auth";
import { useAuth } from "../hooks/useAuth";

/** Landing spot for the Google redirect flow: swaps the one minute ticket in the URL fragment for a session. */
export default function AuthCallback() {
  const { complete } = useAuth();
  const navigate = useNavigate();
  const started = useRef(false);

  useEffect(() => {
    if (started.current) return; // StrictMode runs effects twice; redeem once
    started.current = true;
    const fragment = new URLSearchParams(window.location.hash.slice(1));
    const ticket = fragment.get("ticket");
    const next = safeNext(fragment.get("next"));
    window.history.replaceState(null, "", window.location.pathname); // keep the ticket out of history
    if (!ticket) {
      navigate("/auth", { replace: true });
      return;
    }
    authApi
      .redeemTicket(ticket)
      .then((result) => {
        complete(result);
        navigate(result.created && !next.startsWith("/tools/") ? "/welcome" : next, { replace: true });
      })
      .catch(() => {
        const error = new URLSearchParams({ error: "Google sign in expired. Try again." });
        navigate(`/auth#${error}`, { replace: true });
      });
  }, [complete, navigate]);

  return (
    <div className="boot">
      <div className="orbit-loader">
        <span />
      </div>
    </div>
  );
}