diff --git a/frontend/src/components/header/index.tsx b/frontend/src/components/header/index.tsx index ac1dfbd9e..e5d7819c3 100644 --- a/frontend/src/components/header/index.tsx +++ b/frontend/src/components/header/index.tsx @@ -50,6 +50,11 @@ export const Paths = () => { export const Header = () => { const user = useUser().data; + const logout = () => { + localStorage.removeItem("monitor-auth-token"); + window.location.reload(); + }; + return (
@@ -69,7 +74,7 @@ export const Header = () => { )} {user && ( - )} diff --git a/frontend/src/hooks.ts b/frontend/src/hooks.ts index fac06981f..68f40e390 100644 --- a/frontend/src/hooks.ts +++ b/frontend/src/hooks.ts @@ -1,8 +1,9 @@ import { Types } from "@monitor/client"; import { client } from "./main"; -import { useQuery, useMutation } from "@tanstack/react-query"; +import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useAtomValue, useSetAtom } from "jotai"; import { atomWithStorage } from "jotai/utils"; +import { useNavigate } from "react-router-dom"; export const useRead = (req: T) => useQuery([req], () => client.read(req)); @@ -15,10 +16,18 @@ export const useExecute = () => export const useUser = () => useRead({ type: "GetUser", params: {} }); -export const useLogin = () => - useMutation(client.login, { - onSuccess: (jwt) => localStorage.setItem("monitor-auth-token", jwt ?? ""), +export const useLogin = () => { + const { refetch } = useUser(); + const nav = useNavigate(); + + return useMutation(client.login, { + onSuccess: async (jwt) => { + await refetch(); + localStorage.setItem("monitor-auth-token", jwt ?? ""); + nav("/"); + }, }); +}; // const recents_atom = atomWithStorage< // { type: "Deployment" | "Build" | "Server"; id: string }[] diff --git a/frontend/src/layouts/layout.tsx b/frontend/src/layouts/layout.tsx index 649972321..813980c91 100644 --- a/frontend/src/layouts/layout.tsx +++ b/frontend/src/layouts/layout.tsx @@ -1,5 +1,6 @@ import { Header } from "@components/header"; import { useUser } from "@hooks"; +import { Login } from "@pages/auth/login"; import { Toaster } from "@ui/toast"; import { Outlet, useLocation, useNavigate } from "react-router-dom"; @@ -8,11 +9,6 @@ export const Layout = () => { const path = useLocation().pathname; const nav = useNavigate(); if (isError && !path.includes("login")) nav("/login"); - // const navigate = useNavigate(); - // const path = useLocation().pathname; - // if (isError) return navigate("/login"); - // if ((isError && !path.includes("login")) || !path.includes("signup")) - // navigate("/login"); return ( <> diff --git a/frontend/src/pages/auth/login.tsx b/frontend/src/pages/auth/login.tsx index 31fcf4133..67c947132 100644 --- a/frontend/src/pages/auth/login.tsx +++ b/frontend/src/pages/auth/login.tsx @@ -4,13 +4,10 @@ import { Input } from "@ui/input"; import { Label } from "@ui/label"; import { useLogin } from "@hooks"; import { useState } from "react"; -import { useNavigate } from "react-router-dom"; export const Login = () => { - const { mutateAsync } = useLogin(); - const nav = useNavigate(); + const { mutate, isLoading } = useLogin(); const [creds, set] = useState({ username: "", password: "" }); - const login = async () => !!(await mutateAsync(creds)) && nav("/"); return ( @@ -40,7 +37,11 @@ export const Login = () => { />
-