mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-09-25 00:21:28 +02:00
chore: use auth hook
This commit is contained in:
@@ -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 (
|
||||
<div>
|
||||
{isAuthenticated ? (
|
||||
{user ? (
|
||||
<div>Authenticated: {user?.name}</div>
|
||||
) : (
|
||||
<div>Not Authenticated</div>
|
||||
|
@@ -4,16 +4,14 @@ import Cookies from 'js-cookie';
|
||||
|
||||
export const useAuth = () => {
|
||||
const [user, setUser] = useState<TokenPayload | null>(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 };
|
||||
};
|
||||
|
Reference in New Issue
Block a user