diff --git a/dashboard/src/components/header/index.tsx b/dashboard/src/components/header/index.tsx index c8295ef..8fb3e0a 100644 --- a/dashboard/src/components/header/index.tsx +++ b/dashboard/src/components/header/index.tsx @@ -1,5 +1,6 @@ import { LanguageSwitch } from "@marzneshin/features/language-switch" import { ThemeToggle } from "@marzneshin/features/theme-switch" +import { Logout } from "@marzneshin/features/auth" import { Link } from "@tanstack/react-router" import { FC, PropsWithChildren } from "react" @@ -14,7 +15,8 @@ export const Header: FC = ({ children }) => { {children} -
+
+
diff --git a/dashboard/src/components/nav-dir/index.tsx b/dashboard/src/components/nav-dir/index.tsx index 9ba320a..9c27f79 100644 --- a/dashboard/src/components/nav-dir/index.tsx +++ b/dashboard/src/components/nav-dir/index.tsx @@ -125,7 +125,7 @@ export const NavigationDirectory: FC = ({ sidebar }) = <> {item.label} diff --git a/dashboard/src/features/auth/auth.store.ts b/dashboard/src/features/auth/auth.store.ts index 5955a08..298d22e 100644 --- a/dashboard/src/features/auth/auth.store.ts +++ b/dashboard/src/features/auth/auth.store.ts @@ -1,27 +1,32 @@ +import { fetch } from '@marzneshin/utils'; import { create } from 'zustand'; import { subscribeWithSelector } from 'zustand/middleware'; type AdminStateType = { - isLoggedIn: boolean, + isLoggedIn: () => Promise, getAuthToken: () => string | null; setAuthToken: (token: string) => void; removeAuthToken: () => void; } export const useAuth = create( - subscribeWithSelector((set,) => ({ - isLoggedIn: false, + subscribeWithSelector(() => ({ + isLoggedIn: async () => { + try { + await fetch('/admins'); + return true; + } catch (error) { + return false; + } + }, getAuthToken: () => { return localStorage.getItem('token'); }, setAuthToken: (token: string) => { localStorage.setItem('token', token); - set({ isLoggedIn: true }); }, - removeAuthToken: () => { localStorage.removeItem('token'); - set({ isLoggedIn: false }); }, })) ) diff --git a/dashboard/src/features/auth/index.ts b/dashboard/src/features/auth/index.ts index 08f0a6a..f22eba7 100644 --- a/dashboard/src/features/auth/index.ts +++ b/dashboard/src/features/auth/index.ts @@ -1,2 +1,3 @@ export * from './auth.store'; export * from './login'; +export * from './logout' diff --git a/dashboard/src/features/auth/logout/index.tsx b/dashboard/src/features/auth/logout/index.tsx new file mode 100644 index 0000000..e2e55d4 --- /dev/null +++ b/dashboard/src/features/auth/logout/index.tsx @@ -0,0 +1,22 @@ + +import { Button } from '@marzneshin/components'; +import { useNavigate } from '@tanstack/react-router'; +import { LogOut } from 'lucide-react'; +import { FC, useCallback } from 'react' + +interface LogoutProps { } + +export const Logout: FC = () => { + const navigate = useNavigate({ from: '/' }) + const handleLogout = useCallback( + () => { + navigate({ to: '/login' }) + }, + [navigate], + ) + return ( + + ); +} diff --git a/dashboard/src/features/language-switch/index.tsx b/dashboard/src/features/language-switch/index.tsx index 145256f..59ab3cd 100644 --- a/dashboard/src/features/language-switch/index.tsx +++ b/dashboard/src/features/language-switch/index.tsx @@ -13,8 +13,8 @@ export const LanguageSwitch: FC = () => { return ( - diff --git a/dashboard/src/routes/_auth/login.tsx b/dashboard/src/routes/_auth/login.tsx index d4c89a9..a91eda5 100644 --- a/dashboard/src/routes/_auth/login.tsx +++ b/dashboard/src/routes/_auth/login.tsx @@ -1,11 +1,13 @@ import { Card, CardContent, CardHeader, CardTitle } from '@marzneshin/components'; -import { LoginForm } from '@marzneshin/features/auth'; +import { LoginForm, useAuth } from '@marzneshin/features/auth'; import { createFileRoute } from '@tanstack/react-router' import { FC } from 'react' import { useTranslation } from 'react-i18next'; const LoginPage: FC = () => { const { t } = useTranslation(); + const { removeAuthToken } = useAuth() + removeAuthToken() return (
diff --git a/dashboard/src/routes/_dashboard.tsx b/dashboard/src/routes/_dashboard.tsx index 7baa397..e5c206f 100644 --- a/dashboard/src/routes/_dashboard.tsx +++ b/dashboard/src/routes/_dashboard.tsx @@ -78,7 +78,8 @@ export const Route = createFileRoute('/_dashboard')({ component: () => , beforeLoad: async () => { - if (!useAuth.getState().isLoggedIn) { + const loggedIn = await useAuth.getState().isLoggedIn() + if (!loggedIn) { throw redirect({ to: '/login', }) diff --git a/dashboard/src/utils/fetch.ts b/dashboard/src/utils/fetch.ts index 15d588f..35b8f5b 100644 --- a/dashboard/src/utils/fetch.ts +++ b/dashboard/src/utils/fetch.ts @@ -21,11 +21,3 @@ export const fetcher = ( }; export const fetch = fetcher; - -export const fetchAdminLoader = () => { - return fetch('/admins', { - headers: { - Authorization: `Bearer ${useAuth.getState().getAuthToken()}`, - }, - }); -};