refactor: add new logout function with better info box in the login card

This commit is contained in:
2026-07-10 11:18:25 +02:00
parent 3153076d72
commit 99246e3e3d
3 changed files with 31 additions and 13 deletions
+13 -8
View File
@@ -3,7 +3,7 @@ import { Button, Input } from "@mui/joy";
import { useMutation } from "@tanstack/react-query"; import { useMutation } from "@tanstack/react-query";
import { signInUser } from "../utils/api/auth"; import { signInUser } from "../utils/api/auth";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useNavigate } from "@tanstack/react-router"; import { useNavigate, useSearch } from "@tanstack/react-router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import type { AlertInterface } from "../misc/interfaces"; import type { AlertInterface } from "../misc/interfaces";
import { MyAlert } from "./MyAlert.tsx"; import { MyAlert } from "./MyAlert.tsx";
@@ -11,6 +11,7 @@ import { MyAlert } from "./MyAlert.tsx";
export const LoginCard = () => { export const LoginCard = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const navigate = useNavigate(); const navigate = useNavigate();
const search: { loggedOut?: boolean } = useSearch({ from: "/login" });
const [alert, setAlert] = useState<AlertInterface>({ const [alert, setAlert] = useState<AlertInterface>({
isAlert: false, isAlert: false,
type: "neutral", type: "neutral",
@@ -19,12 +20,16 @@ export const LoginCard = () => {
}); });
useEffect(() => { useEffect(() => {
setAlert({ console.log(search);
isAlert: true, if (search.loggedOut) {
type: "primary", setAlert({
header: t("success"), isAlert: true,
text: t("logout-success-text"), type: "primary",
}); header: t("success"),
text: t("logout-success-text"),
});
}
void navigate({ to: "/login" });
}, []); }, []);
const form = useForm({ const form = useForm({
@@ -53,7 +58,7 @@ export const LoginCard = () => {
header: "", header: "",
text: "", text: "",
}); });
navigate({ to: "/app/inventory" }); void navigate({ to: "/app/inventory" });
} }
}, },
onError: (error: unknown) => { onError: (error: unknown) => {
+4 -5
View File
@@ -14,10 +14,12 @@ import { useMatchRoute, useNavigate } from "@tanstack/react-router";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { changeTranslation } from "../utils/uxFncs"; import { changeTranslation } from "../utils/uxFncs";
import { useColorScheme } from "@mui/joy/styles"; import { useColorScheme } from "@mui/joy/styles";
import { useLogout } from "../hooks/useLogout.ts";
export const Sidebar = () => { export const Sidebar = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const navigate = useNavigate(); const navigate = useNavigate();
const { logout } = useLogout();
const matchRoute = useMatchRoute(); const matchRoute = useMatchRoute();
const { mode, setMode } = useColorScheme(); const { mode, setMode } = useColorScheme();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
@@ -29,7 +31,7 @@ export const Sidebar = () => {
!!matchRoute({ to, fuzzy: false }) ? "soft" : "plain"; !!matchRoute({ to, fuzzy: false }) ? "soft" : "plain";
const handleNavigate = (to: string) => { const handleNavigate = (to: string) => {
navigate({ to }); void navigate({ to });
setIsOpen(false); setIsOpen(false);
}; };
@@ -154,10 +156,7 @@ export const Sidebar = () => {
{t("settings")} {t("settings")}
</Button> </Button>
<Button <Button
onClick={() => { onClick={logout}
Cookies.remove("token");
handleNavigate("/login?logout=true");
}}
color="danger" color="danger"
startDecorator={<ExitToAppIcon />} startDecorator={<ExitToAppIcon />}
className={btnClass} className={btnClass}
+14
View File
@@ -0,0 +1,14 @@
import Cookies from "js-cookie";
import { useNavigate } from "@tanstack/react-router";
export const useLogout = () => {
const navigate = useNavigate();
const logout = () => {
console.log("LOGOUT");
Cookies.remove("token");
void navigate({ to: "/login", search: { loggedOut: true } });
};
return { logout };
};