feat: add authentication

This commit is contained in:
2025-03-01 23:22:18 +07:00
parent b53859ae23
commit f8e65ccc0e
21 changed files with 375 additions and 27 deletions
+47 -14
View File
@@ -4,6 +4,7 @@ import {
HardDrive,
KeySquare,
LayoutDashboard,
LogOut,
Palette,
} from "lucide-react";
import { Dropdown, Menu } from "react-daisyui";
@@ -12,6 +13,10 @@ import Button from "../ui/button";
import { themes } from "@/app/themes";
import appStore from "@/stores/app-store";
import garageLogo from "@/assets/garage-logo.svg";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import api from "@/lib/api";
import { toast } from "sonner";
import { useAuth } from "@/hooks/useAuth";
const pages = [
{ icon: LayoutDashboard, title: "Dashboard", path: "/", exact: true },
@@ -22,6 +27,7 @@ const pages = [
const Sidebar = () => {
const { pathname } = useLocation();
const auth = useAuth();
return (
<aside className="bg-base-100 border-r border-base-300/30 w-[80%] md:w-[250px] flex flex-col items-stretch overflow-hidden h-full">
@@ -57,23 +63,50 @@ const Sidebar = () => {
})}
</Menu>
<Dropdown className="my-2 mx-4" vertical="top">
<Dropdown.Toggle button={false}>
<Button icon={Palette} color="ghost">
Theme
</Button>
</Dropdown.Toggle>
<div className="py-2 px-4 flex items-center gap-2">
<Dropdown vertical="top">
<Dropdown.Toggle button={false}>
<Button icon={Palette} color="ghost">
{!auth.isEnabled ? "Theme" : null}
</Button>
</Dropdown.Toggle>
<Dropdown.Menu className="max-h-[200px] overflow-y-auto">
{themes.map((theme) => (
<Dropdown.Item key={theme} onClick={() => appStore.setTheme(theme)}>
{ucfirst(theme)}
</Dropdown.Item>
))}
</Dropdown.Menu>
</Dropdown>
<Dropdown.Menu className="max-h-[500px] overflow-y-auto">
{themes.map((theme) => (
<Dropdown.Item
key={theme}
onClick={() => appStore.setTheme(theme)}
>
{ucfirst(theme)}
</Dropdown.Item>
))}
</Dropdown.Menu>
</Dropdown>
{auth.isEnabled ? <LogoutButton /> : null}
</div>
</aside>
);
};
const LogoutButton = () => {
const queryClient = useQueryClient();
const logout = useMutation({
mutationFn: () => api.post("/auth/logout"),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["auth"] });
},
onError: (err) => {
toast.error(err?.message || "Unknown error");
},
});
return (
<Button className="flex-1" icon={LogOut} onClick={() => logout.mutate()}>
Logout
</Button>
);
};
export default Sidebar;
+18 -1
View File
@@ -1,5 +1,22 @@
import { useAuth } from "@/hooks/useAuth";
import { Navigate, Outlet } from "react-router-dom";
const AuthLayout = () => {
return <div>AuthLayout</div>;
const auth = useAuth();
if (auth.isLoading) {
return null;
}
if (auth.isAuthenticated) {
return <Navigate to="/" replace />;
}
return (
<div className="min-h-svh flex items-center justify-center">
<Outlet />
</div>
);
};
export default AuthLayout;
+11 -1
View File
@@ -1,15 +1,17 @@
import { PageContext } from "@/context/page-context";
import { Suspense, useContext, useEffect } from "react";
import { Outlet, useLocation, useNavigate } from "react-router-dom";
import { Navigate, Outlet, useLocation, useNavigate } from "react-router-dom";
import Sidebar from "../containers/sidebar";
import { ArrowLeft, MenuIcon } from "lucide-react";
import Button from "../ui/button";
import { useDisclosure } from "@/hooks/useDisclosure";
import { Drawer } from "react-daisyui";
import { useAuth } from "@/hooks/useAuth";
const MainLayout = () => {
const sidebar = useDisclosure();
const { pathname } = useLocation();
const auth = useAuth();
useEffect(() => {
if (sidebar.isOpen) {
@@ -17,6 +19,14 @@ const MainLayout = () => {
}
}, [pathname]);
if (auth.isLoading) {
return null;
}
if (!auth.isAuthenticated) {
return <Navigate to="/auth/login" />;
}
return (
<Drawer
open={sidebar.isOpen}
+1
View File
@@ -4,6 +4,7 @@ import { Button as BaseButton } from "react-daisyui";
import { Link } from "react-router-dom";
type ButtonProps = ComponentPropsWithoutRef<typeof BaseButton> & {
type?: HTMLButtonElement["type"];
href?: string;
target?: "_blank" | "_self" | "_parent" | "_top";
icon?: LucideIcon;