diff --git a/index.html b/index.html index a094d6a..8810eea 100644 --- a/index.html +++ b/index.html @@ -2,9 +2,9 @@ - + - Vite + React + TS + Livro Livre Admin diff --git a/public/logo.png b/public/logo.png new file mode 100644 index 0000000..90ec9ab Binary files /dev/null and b/public/logo.png differ diff --git a/public/vite.svg b/public/vite.svg deleted file mode 100644 index e7b8dfb..0000000 --- a/public/vite.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index 7a741b8..985ad8a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -16,6 +16,7 @@ function App() { } /> + } /> } /> } /> } /> diff --git a/src/PrivateRoute.tsx b/src/PrivateRoute.tsx index caf38f0..190837a 100644 --- a/src/PrivateRoute.tsx +++ b/src/PrivateRoute.tsx @@ -4,12 +4,12 @@ import { useEffect } from "react"; const PrivateRoute = ({ children }: { children: any }) => { const navigate = useNavigate(); - const { token } = useAuth(); + const { isAuthenticated } = useAuth(); useEffect(() => { - if (token) return; + if (isAuthenticated) return; navigate('/login'); - }) + }, [isAuthenticated]) return children; } diff --git a/src/hooks/useApi/index.tsx b/src/hooks/useApi/index.tsx index 2da0a66..eae67ea 100644 --- a/src/hooks/useApi/index.tsx +++ b/src/hooks/useApi/index.tsx @@ -45,7 +45,7 @@ const useApi = () => { getUsers: (data: { perPage: number; page: number - }): Promise<{ data: { + }, token: string | null): Promise<{ data: { items: User[], total: number, } }> => { @@ -55,6 +55,9 @@ const useApi = () => { params: { perPage: data.perPage, page: data.page, + }, + headers: { + Authorization: `Bearer ${token}`, } }) .then((res) => resolve(res)) diff --git a/src/pages/SignIn/SignInForm/index.tsx b/src/pages/SignIn/SignInForm/index.tsx index a5e1571..ee96c4a 100644 --- a/src/pages/SignIn/SignInForm/index.tsx +++ b/src/pages/SignIn/SignInForm/index.tsx @@ -22,7 +22,7 @@ function SignInForm() { formState: { errors, isValid }, } = useForm(); - const { signIn, token } = useAuth(); + const { signIn, isAuthenticated } = useAuth(); const onSubmit = handleSubmit(async (data: FormValues) => { setLoading(true); @@ -34,9 +34,9 @@ function SignInForm() { }) useEffect(() => { - if (!token) return; + if (!isAuthenticated) return; navigate('/inicio'); - }, [token]) + }, [isAuthenticated]) return (
diff --git a/src/pages/SignIn/index.tsx b/src/pages/SignIn/index.tsx index 5707465..f08def8 100644 --- a/src/pages/SignIn/index.tsx +++ b/src/pages/SignIn/index.tsx @@ -1,8 +1,18 @@ import { Box, Center, Stack } from '@chakra-ui/react'; import SignInForm from './SignInForm'; import SignInHeader from './SignInHeader'; +import { useNavigate } from 'react-router'; +import { useAuth } from '../../hooks/useAuth'; +import { useEffect } from 'react'; function SignIn() { + const navigate = useNavigate(); + const { isAuthenticated } = useAuth(); + + useEffect(() => { + if (isAuthenticated) navigate('/inicio'); + }, [isAuthenticated]) + return (
diff --git a/src/pages/Users/index.tsx b/src/pages/Users/index.tsx index 6a4b76f..27c53d9 100644 --- a/src/pages/Users/index.tsx +++ b/src/pages/Users/index.tsx @@ -9,6 +9,7 @@ import { PaginationPrevTrigger, PaginationRoot, } from "../../components/ui/pagination"; +import { useAuth } from '../../hooks/useAuth'; function Users() { const [users, setUsers] = useState([]); @@ -16,9 +17,10 @@ function Users() { const [page, setPage] = useState(1); const { getUsers } = useApi(); + const { token } = useAuth(); const fetchUsers = async () => { - const { data } = await getUsers({ perPage: 10, page: page - 1 }); + const { data } = await getUsers({ perPage: 10, page: page - 1 }, token); setUsers(data.items); setUsersCount(data.total); }