feat: api client and natural login and logoff

This commit is contained in:
2025-05-05 16:53:18 +03:00
parent db73f498f6
commit 0055e09806
5 changed files with 215 additions and 26 deletions

View File

@@ -2,6 +2,7 @@ import Button from "@/components/ui/Button";
import { withTitle } from "@/constructors/Component";
import { UrlsTitle } from "@/enums/urls";
import { useAppContext } from "@/providers/AuthProvider";
import apiClient from "@/services/api";
import { cn } from "@/utils/class-merge";
import { calculatePoints, getCurrentStatus } from "@/utils/status-system";
import { ArrowRightStartOnRectangleIcon, Cog8ToothIcon } from "@heroicons/react/24/outline";
@@ -38,6 +39,18 @@ const ProfileSettings: FunctionComponent = () => {
return () => window.removeEventListener("storage", handleStorage);
}, []);
const handleLogout = async () => {
try {
await apiClient("/api/logout/", { method: "POST", needsCsrf: true }, isLoggedIn);
isLoggedIn.value = false;
localStorage.removeItem("loggedIn");
localStorage.removeItem("user");
route("/login", true);
} catch (error) {
console.error("Logout failed:", error);
}
};
return (
<div class={classes.container}>
<div class="flex w-full flex-col items-center rounded-[4rem] bg-[linear-gradient(180.00deg,rgb(251,194,199),rgba(206,232,251,0.72)_100%)] px-7 py-5 shadow-[0px_4px_4px_0px_rgba(0,0,0,0.25)] md:flex-row">
@@ -64,11 +77,7 @@ const ProfileSettings: FunctionComponent = () => {
</Button>
<Button
className="flex flex-row items-center justify-center gap-2 bg-[linear-gradient(180.00deg,rgba(246,255,211,0.7),rgba(195,229,253,0.7)_100%)]"
onClick={() => {
isLoggedIn.value = false;
localStorage.setItem("loggedIn", "false");
route("/login", true);
}}
onClick={handleLogout}
>
<ArrowRightStartOnRectangleIcon class="size-8" /> Выйти
</Button>