Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/web/components/credentials/CredentialsHome.tsx
blob: 6ffc42da77dd291faa720f0edd3a675d65527871 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

"use client";

import { useEffect, useState } from "react";
import { callOAuthApi, type SessionView } from "@/lib/credentials";
import { format, getCredentialsMessages } from "@/lib/credentials-messages";
import type { Locale } from "@/lib/localization";

type HomeState =
  | { kind: "loading" }
  | { kind: "signedOut" }
  | { kind: "signedIn"; accountName: string };

/** The home page of credentials.progmasoft.com: where to go, signed in or not. */
export function CredentialsHome({
  locale,
  consoleOrigin,
}: {
  locale: Locale;
  consoleOrigin: string;
}) {
  const { home, common } = getCredentialsMessages(locale);
  const [state, setState] = useState<HomeState>({ kind: "loading" });

  useEffect(() => {
    const controller = new AbortController();
    void callOAuthApi<SessionView>("/session", {
      signal: controller.signal,
    }).then((result) => {
      setState(
        result.kind === "ok"
          ? { kind: "signedIn", accountName: result.value.accountName }
          : { kind: "signedOut" },
      );
    });
    return () => controller.abort();
  }, []);

  async function signOut() {
    await callOAuthApi("/signout", { method: "POST" });
    setState({ kind: "signedOut" });
  }

  return (
    <>
      <div className="credentials-panel">
        {state.kind === "loading" && (
          <p className="credentials-note">{common.loading}</p>
        )}
        {state.kind === "signedOut" && (
          <>
            <p>{home.signedOut}</p>
            <div className="credentials-actions">
              <a className="button" href="/signin">
                {common.signIn}
              </a>
            </div>
          </>
        )}
        {state.kind === "signedIn" && (
          <>
            <p>{format(common.signedInAs, { name: state.accountName })}</p>
            <ul className="credentials-list">
              <li>
                <a href={`${consoleOrigin}/credentials`}>{common.developers}</a>
                <span className="credentials-meta">{home.developersText}</span>
              </li>
              <li>
                <a href="/account/applications">{common.allowedApplications}</a>
                <span className="credentials-meta">{home.allowedText}</span>
              </li>
            </ul>
            <div className="credentials-actions">
              <button
                className="button button-secondary"
                type="button"
                onClick={signOut}
              >
                {common.signOut}
              </button>
            </div>
          </>
        )}
      </div>
      <p className="credentials-note credentials-footnote">
        {home.discovery}:{" "}
        <a href="/.well-known/openid-configuration">
          /.well-known/openid-configuration
        </a>
      </p>
    </>
  );
}