diff --git a/src/components/SignIn.tsx b/src/components/SignIn.tsx index 44238b7..e78ae7f 100644 --- a/src/components/SignIn.tsx +++ b/src/components/SignIn.tsx @@ -11,15 +11,18 @@ import { Button } from '@/components/ui/button'; import { Label } from '@/components/ui/label'; import { Input } from '@/components/ui/input'; import { useToast } from '@/components/ui/use-toast'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; import NProgress from 'nprogress'; import { useTranslation } from 'react-i18next'; +import { LogIn } from 'lucide-react'; +import authStore from '@/stores/auth'; -export default function SignIn({ setIsAuth, setStoreAuth }) { +export default function SignIn() { const { t } = useTranslation(); const { toast } = useToast(); const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); + const { login } = authStore(); const signIn = () => new Promise((resolve) => { NProgress.start(); @@ -27,11 +30,9 @@ export default function SignIn({ setIsAuth, setStoreAuth }) { setTimeout(resolve, 1000); }).then(() => { NProgress.done(); + login(); setLoading(false); setOpen(false); - setIsAuth(true); - localStorage.setItem('isAuth', true); - setStoreAuth(true); toast({ description: 'Signed in successfully!', }); @@ -39,14 +40,14 @@ export default function SignIn({ setIsAuth, setStoreAuth }) { const handleEmailKeyPress = (e) => { if (e.key === 'Enter') document.getElementById('password').focus(); }; - const handlePasswordKeyPress = (e) => { - if (e.key === 'Enter') signIn(); + const handlePasswordKeyPress = async (e) => { + if (e.key === 'Enter') await signIn(); }; return ( - + diff --git a/src/components/layouts/header/index.tsx b/src/components/layouts/header/index.tsx index 0c67182..0d227cf 100644 --- a/src/components/layouts/header/index.tsx +++ b/src/components/layouts/header/index.tsx @@ -1,48 +1,31 @@ -import { Link, useNavigate } from 'react-router-dom'; +import { Link } from 'react-router-dom'; import Menu from '@/components/layouts/header/menu'; import MobileMenu from '@/components/layouts/header/mobileMenu'; import { useTranslation } from 'react-i18next'; import { - Globe, Moon, Sun, User, + Globe, LogOut, Moon, Sun, User, } from 'lucide-react'; import useLanguage from '@/stores/language'; -import { Button } from '@/components/ui/button'; import SignIn from '@/components/SignIn'; import { useDarkMode } from '@/lib/dark-mode'; -import { useState, useEffect } from 'react'; -import { useLocalStorage } from 'usehooks-ts'; import { useToast } from '@/components/ui/use-toast'; +import authStore from '@/stores/auth'; function Header() { const { t } = useTranslation(); const { toast } = useToast(); - const { isDarkMode, toggle } = useDarkMode(); + const { toggle } = useDarkMode(); const { toggleLang } = useLanguage(); - const [isAuth, setIsAuth] = useState(false); - const [storeauth, setStoreAuth] = useLocalStorage('isAuth', false); - - const navigate = useNavigate(); + const { logout, isAuth } = authStore(); const menuItems = [{ path: '/example', name: t('menu.example') }]; const signOut = () => { - setIsAuth(false); - setStoreAuth(false); - localStorage.setItem('isAuth', 'false'); + logout(); toast({ description: 'Signed out successfully!', }); }; - useEffect(() => { - const storedAuth = localStorage.getItem('isAuth'); - if (storedAuth === 'true') { - setIsAuth(true); - setStoreAuth(true); - } else { - navigate('/'); - } - }, [isAuth]); - return (
diff --git a/src/main.tsx b/src/main.tsx index 526411d..276f68f 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,7 +4,6 @@ import { HelmetProvider } from 'react-helmet-async'; import '@/lib/locales'; import { createRoot } from 'react-dom/client'; import { Routes } from '@generouted/react-router'; -import NProgress from 'nprogress'; import 'nprogress/nprogress.css'; import '@/style/nprogress.css'; diff --git a/src/pages/(auth)/_layout.tsx b/src/pages/(auth)/_layout.tsx new file mode 100644 index 0000000..e6a3828 --- /dev/null +++ b/src/pages/(auth)/_layout.tsx @@ -0,0 +1,20 @@ +import { useEffect } from 'react'; +import authStore from '@/stores/auth'; +import { useNavigate, Outlet } from 'react-router-dom'; + +export default function Index() { + const { isAuth } = authStore(); + const nav = useNavigate(); + + useEffect(() => { + if (!isAuth) { + nav('/', { replace: true }); + } + }, [isAuth]); + + return ( +
+ +
+ ); +} diff --git a/src/stores/auth.tsx b/src/stores/auth.tsx new file mode 100644 index 0000000..996d2c6 --- /dev/null +++ b/src/stores/auth.tsx @@ -0,0 +1,24 @@ +import { create } from 'zustand'; + +type Auth = { + isAuth: boolean + login: () => void + logout: () => void +} + +const authStore = create((set) => ({ + isAuth: localStorage.getItem('isAuth') === 'true' || false, + + login: () => set(() => { + localStorage.setItem('isAuth', 'true'); + return { isAuth: true }; + }), + + logout: () => set(() => { + localStorage.removeItem('isAuth'); + return { isAuth: false }; + }), + +})); + +export default authStore;