From 00133369afa46b7c30cc38675efb9c1e36e396c4 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Fri, 31 Mar 2023 00:13:45 +0600 Subject: [PATCH] chore: use auth hook --- src/components/Login/account-nav.tsx | 19 +++---------------- src/hooks/use-auth.ts | 8 +++----- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/components/Login/account-nav.tsx b/src/components/Login/account-nav.tsx index a35798911..b1841e7b0 100644 --- a/src/components/Login/account-nav.tsx +++ b/src/components/Login/account-nav.tsx @@ -1,26 +1,13 @@ -import Cookies from 'js-cookie'; -import { useEffect, useState } from 'preact/hooks'; -import { TOKEN_COOKIE_NAME } from '../../lib/utils'; import { useAuth } from '../../hooks/use-auth'; export default function AccountNavigation() { - const [isAuthenticated, setIsAuthenticated] = useState(false); - const { user, status } = useAuth(); + const { user, isLoading } = useAuth(); - useEffect(() => { - const token = Cookies.get(TOKEN_COOKIE_NAME); - if (token) { - setIsAuthenticated(true); - } else { - setIsAuthenticated(false); - } - }, []); - - console.log('user', user, status); + console.log('user', user, isLoading); return (
- {isAuthenticated ? ( + {user ? (
Authenticated: {user?.name}
) : (
Not Authenticated
diff --git a/src/hooks/use-auth.ts b/src/hooks/use-auth.ts index 08d18eac5..fbfbbbb5a 100644 --- a/src/hooks/use-auth.ts +++ b/src/hooks/use-auth.ts @@ -4,16 +4,14 @@ import Cookies from 'js-cookie'; export const useAuth = () => { const [user, setUser] = useState(null); - const [status, setStatus] = useState<'loading' | 'success' | 'error'>( - 'loading' - ); + const [isLoading, setIsLoading] = useState(true); useEffect(() => { const token = Cookies.get(TOKEN_COOKIE_NAME); const payload = token ? decodeToken(token) : null; setUser(payload); - setStatus('success'); + setIsLoading(false); }, []); - return { user, status }; + return { user, isLoading }; };