Files
golunteer/client/src/app/Main.tsx
z1glr add1516362
All checks were successful
Create and publish the Docker image / build-and-push-image (push) Successful in 2m4s
added content for legal sites
2025-10-04 13:18:24 +00:00

72 lines
1.5 KiB
TypeScript

"use client";
import Loading from "@/components/Loading";
import { apiCall } from "@/lib";
import zustand, { StateUser } from "@/Zustand";
import { usePathname, useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
enum AuthState {
LoggedIn,
LoginScreen,
NoLongSite,
Unauthorized,
Loading,
}
export default function Main({ children }: { children: React.ReactNode }) {
const [auth, setAuth] = useState(AuthState.Loading);
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
void (async () => {
let loggedIn = false;
const welcomeResult = await apiCall<StateUser>("GET", "welcome");
if (welcomeResult.ok) {
try {
const response = await welcomeResult.json();
if (response.userName !== undefined && response.userName !== "") {
zustand.getState().patch({ user: response });
loggedIn = true;
}
} catch {}
} else {
zustand.getState().reset();
}
switch (pathname) {
case "/datenschutz":
case "/impressum":
setAuth(AuthState.NoLongSite);
return;
case "/login":
if (loggedIn) {
router.push("/");
} else {
setAuth(AuthState.LoginScreen);
}
return;
default:
setAuth(AuthState.Unauthorized);
router.push("/login");
}
})();
}, [pathname, router]);
switch (auth) {
case AuthState.Loading:
return <Loading />;
case AuthState.LoggedIn:
case AuthState.LoginScreen:
case AuthState.NoLongSite:
return children;
case AuthState.Unauthorized:
return "";
}
}