prepared for login

This commit is contained in:
z1glr
2025-01-08 00:42:06 +00:00
parent f0ad6a3b64
commit be8705286e
9 changed files with 133 additions and 61 deletions

55
client/src/app/Main.tsx Normal file
View File

@@ -0,0 +1,55 @@
"use client";
import { apiCall } from "@/lib";
import { Spinner } from "@nextui-org/spinner";
import { usePathname, useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
enum AuthState {
Loading,
LoginScreen,
Unauthorized,
LoggedIn,
}
export default function Main({ children }: { children: React.ReactNode }) {
const [status, setStatus] = useState(AuthState.Loading);
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
void (async () => {
if (pathname === "/login") {
setStatus(AuthState.LoginScreen);
} else {
const welcomeResult = await apiCall<{
userName: string;
loggedIn: boolean;
}>("GET", "welcome");
if (!welcomeResult.ok) {
router.push("/login");
} else {
const response = await welcomeResult.json();
if (response.loggedIn) {
setStatus(AuthState.LoggedIn);
} else {
setStatus(AuthState.Unauthorized);
}
}
}
})();
});
switch (status) {
case AuthState.Loading:
return <Spinner label="Loading..." />;
case AuthState.LoggedIn:
case AuthState.LoginScreen:
return children;
case AuthState.Unauthorized:
return "";
}
}