diff --git a/frontend/index.html b/frontend/index.html
index 675d9f9..162f415 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -8,18 +8,23 @@
-
-
-
-
+
+
-
-
diff --git a/frontend/src/auth.ts b/frontend/src/auth.ts
index 8fdedb6..cd5dc87 100644
--- a/frontend/src/auth.ts
+++ b/frontend/src/auth.ts
@@ -1,9 +1,12 @@
import { showError } from "@app/toast";
-import client from '@app/api';
-import cookie from 'js-cookie';
+import client from "@app/api";
+import cookie from "js-cookie";
import { ensureWindowState } from "@app/utils";
+import { navigateTo } from "./routing";
-cookie.remove('pkce');
+cookie.remove("pkce");
+const headerProfile =
+ document.querySelector
("#header-profile")!;
ensureWindowState();
window.__state.user ??= null;
@@ -16,14 +19,15 @@ export type User = {
loginName?: string;
providerId?: string;
providerUser?: string;
- }
+ };
};
-declare module 'ft_state' {
+declare module "ft_state" {
interface State {
- user: User | null,
+ user: User | null;
+ _headerProfile: boolean;
}
-};
+}
export function getUser(): Readonly | null {
return window.__state.user;
@@ -35,11 +39,20 @@ export function isLogged(): boolean {
export function setUser(newUser: User | null) {
window.__state.user = newUser;
+ updateHeaderProfile();
+}
+
+export function updateHeaderProfile() {
+ if (window.__state.user !== null) {
+ headerProfile.innerText = window.__state.user.name;
+ } else {
+ headerProfile.innerText = "Login";
+ }
}
export async function updateUser(): Promise | null> {
try {
- let res = await client.getUser({ user: 'me' });
+ let res = await client.getUser({ user: "me" });
if (res.kind === "success") {
setUser(res.payload);
@@ -63,5 +76,17 @@ export async function updateUser(): Promise | null> {
}
}
-Object.assign(window as any, { getUser, setUser, updateUser, isLogged });
+window.__state._headerProfile ??= false;
+if (!window.__state._headerProfile) {
+ headerProfile.addEventListener("click", () => {
+ if (window.__state.user === null) {
+ navigateTo(`/login?returnTo=${encodeURIComponent(window.location.pathname)}`);
+ } else {
+ navigateTo("/profile");
+ }
+ });
+ window.__state._headerProfile = true;
+}
+updateHeaderProfile();
+Object.assign(window as any, { getUser, setUser, updateUser, isLogged });