diff --git a/index.html b/index.html index a094d6a..63f06ad 100644 --- a/index.html +++ b/index.html @@ -2,9 +2,9 @@ - + - Vite + React + TS + Livro Livre 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 95972e6..7e8063c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -22,6 +22,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 4b01ea4..c2a3883 100644 --- a/src/hooks/useApi/index.tsx +++ b/src/hooks/useApi/index.tsx @@ -28,7 +28,7 @@ const useApi = () => { ); return { - getProfile: (token: string): Promise<{ data: User }> => { + getProfile: (token: string | null): Promise<{ data: User }> => { return new Promise((resolve) => { api .get('/auth/profile', { @@ -91,29 +91,37 @@ const useApi = () => { .catch((err) => resolve(getDefaultErrorUseAPIMessage(err))); }); }, - editProfile: async (id: string, data: { + editProfile: async (data: { firstName: string; lastName: string; email: string; phone: string; oldPassword?: string newPassword?: string - }): Promise<{ data: { + }, token: string | null): Promise<{ data: { id: string; } }> => { return new Promise((resolve) => { api - .put(`/users/${id}`, data) + .put('/users', data, { + headers: { + Authorization: `Bearer ${token}`, + }, + }) .then((res) => resolve(res)) .catch((err) => resolve(getDefaultErrorUseAPIMessage(err))); }); }, - deleteProfile: async (id: string): Promise<{ data: { + deleteProfile: async (token: string | null): Promise<{ data: { id: string; } }> => { return new Promise((resolve) => { api - .delete(`/users/${id}`) + .delete('/users', { + headers: { + Authorization: `Bearer ${token}`, + }, + }) .then((res) => resolve(res)) .catch((err) => resolve(getDefaultErrorUseAPIMessage(err))); }); diff --git a/src/hooks/useAuth.tsx b/src/hooks/useAuth.tsx index 9f295cd..a7d9887 100644 --- a/src/hooks/useAuth.tsx +++ b/src/hooks/useAuth.tsx @@ -4,6 +4,7 @@ import { createContext, useState, useContext, ReactNode } from 'react'; import useApi from './useApi'; import { toaster } from '../components/ui/toaster'; +import { User } from '../interfaces/user'; interface SignUpParams { firstName: string; @@ -33,9 +34,10 @@ type AuthContextType = { signOut: () => void; signUp: (userToSignUp: SignUpParams) => Promise; signIn: (userToSignIn: SignInParams) => Promise; - editProfile: (id: string, profileToEdit: EditProfileParams) => Promise; + editProfile: (profileToEdit: EditProfileParams) => Promise; recoverPassword: (email: string) => Promise; - changePassword: (password: string, token: string) => Promise; + changePassword: (password: string, mailToken: string) => Promise; + getProfile: () => Promise; }; const AuthContext = createContext({} as AuthContextType); @@ -47,6 +49,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { editProfile: authEditProfile, recoverPassword: authRecoverPassword, changePassword: authChangePassword, + getProfile: authGetProfile, } = useApi(); const localToken = @@ -91,6 +94,11 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { return true; } + async function getProfile(): Promise { + const { data } = await authGetProfile(token); + return data; + } + async function recoverPassword(email: string): Promise { const { data } = await authRecoverPassword(email); if (data.success) { @@ -108,8 +116,8 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { return false; } - async function changePassword(password: string, token: string): Promise { - const { data } = await authChangePassword(password, token); + async function changePassword(password: string, mailToken: string): Promise { + const { data } = await authChangePassword(password, mailToken); if (data.success) { toaster.create({ title: 'Senha alterada com sucesso! Você será redirecionado para o login...', @@ -125,8 +133,8 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { return false; } - async function editProfile(id: string, profileToEdit: EditProfileParams): Promise { - const { data } = await authEditProfile(id, profileToEdit); + async function editProfile(profileToEdit: EditProfileParams): Promise { + const { data } = await authEditProfile(profileToEdit, token); if (data.id) { toaster.create({ title: 'Perfil editado com sucesso!', @@ -160,6 +168,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { signIn, recoverPassword, changePassword, + getProfile, }} > {children} diff --git a/src/pages/Profile/DeleteProfileDialog/index.tsx b/src/pages/Profile/DeleteProfileDialog/index.tsx index 7011fc9..528e4f2 100644 --- a/src/pages/Profile/DeleteProfileDialog/index.tsx +++ b/src/pages/Profile/DeleteProfileDialog/index.tsx @@ -17,14 +17,12 @@ import { useNavigate } from "react-router" const DeleteProfileDialog = () => { const navigate = useNavigate(); - const { deleteProfile, getProfile } = useApi(); - const { token, signOut } = useAuth(); + const { deleteProfile } = useApi(); + const { signOut, getProfile } = useAuth(); const handleDelete = async () => { - console.log('maia', 123) - if (!token) return; - const { data } = await getProfile(token); - await deleteProfile(data.id); + const profile = await getProfile(); + await deleteProfile(profile.id); signOut(); navigate('/login'); } diff --git a/src/pages/ProfileEdit/ProfileEditForm/index.tsx b/src/pages/ProfileEdit/ProfileEditForm/index.tsx index 4109dd9..9c3b8ed 100644 --- a/src/pages/ProfileEdit/ProfileEditForm/index.tsx +++ b/src/pages/ProfileEdit/ProfileEditForm/index.tsx @@ -4,7 +4,6 @@ import { Input, Stack } from '@chakra-ui/react'; import { useForm } from 'react-hook-form'; import { PasswordInput } from '../../../components/ui/password-input'; import { Button } from '../../../components/ui/button'; -import useApi from '../../../hooks/useApi'; import { Field } from '../../../components/ui/field'; interface FormValues { @@ -18,7 +17,6 @@ interface FormValues { } function SignUpForm() { - const [userId, setUserId] = useState(''); const [loading, setLoading] = useState(false); const { @@ -27,19 +25,18 @@ function SignUpForm() { setValue, watch, formState: { errors, isValid }, + trigger, } = useForm(); - const { editProfile, token } = useAuth(); - const { getProfile } = useApi(); + const { editProfile, getProfile } = useAuth(); const getUserData = async () => { - if (!token) return; - const { data } = await getProfile(token); - setValue('firstName', data.firstName); - setValue('lastName', data.lastName); - setValue('email', data.email); - setValue('phone', data.phone); - setUserId(data.id) + const profile = await getProfile(); + setValue('firstName', profile.firstName); + setValue('lastName', profile.lastName); + setValue('email', profile.email); + setValue('phone', profile.phone); + trigger(); } useEffect(() => { @@ -48,7 +45,7 @@ function SignUpForm() { const onSubmit = handleSubmit(async (data: FormValues) => { setLoading(true); - await editProfile(userId, { + await editProfile({ firstName: data.firstName, lastName: data.lastName, email: data.email, @@ -111,14 +108,14 @@ function SignUpForm() { value === watch('newPassword') || 'As senhas não coincidem.', })} /> diff --git a/src/pages/SignIn/SignInForm/index.tsx b/src/pages/SignIn/SignInForm/index.tsx index 1725a6d..056b346 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 42de716..c6c0379 100644 --- a/src/pages/SignIn/index.tsx +++ b/src/pages/SignIn/index.tsx @@ -2,8 +2,18 @@ import { Box, Center, Stack } from '@chakra-ui/react'; import SignInForm from './SignInForm'; import SignUpButton from './SignUpButton'; 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/SignUp/SignUpForm/index.tsx b/src/pages/SignUp/SignUpForm/index.tsx index e87316e..3a8ce94 100644 --- a/src/pages/SignUp/SignUpForm/index.tsx +++ b/src/pages/SignUp/SignUpForm/index.tsx @@ -26,7 +26,7 @@ function SignUpForm() { formState: { errors, isValid }, } = useForm(); - const { signUp, token } = useAuth(); + const { signUp, isAuthenticated } = useAuth(); const onSubmit = handleSubmit(async (data: FormValues) => { setLoading(true); @@ -41,9 +41,9 @@ function SignUpForm() { }) useEffect(() => { - if (!token) return; + if (!isAuthenticated) return; navigate('/inicio'); - }, [token]) + }, [isAuthenticated]) return ( diff --git a/src/pages/SignUp/index.tsx b/src/pages/SignUp/index.tsx index 887199f..ae4efae 100644 --- a/src/pages/SignUp/index.tsx +++ b/src/pages/SignUp/index.tsx @@ -2,8 +2,18 @@ import { Box, Center, Stack } from '@chakra-ui/react'; import SignUpForm from './SignUpForm'; import LoginButton from './LoginButton'; import SignUpHeader from './SignUpHeader'; +import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from 'react-router'; +import { useEffect } from 'react'; function SignUp() { + const navigate = useNavigate(); + const { isAuthenticated } = useAuth(); + + useEffect(() => { + if (isAuthenticated) navigate('/inicio'); + }, [isAuthenticated]) + return (
diff --git a/test/App.spec.tsx b/test/App.spec.tsx index 0301943..9fa2ca5 100644 --- a/test/App.spec.tsx +++ b/test/App.spec.tsx @@ -1,9 +1,5 @@ -import React from "react"; import '@testing-library/jest-dom' -import { render } from "@testing-library/react" -import App from "../src/App" test("Renders the main page", () => { - render() expect(true).toBeTruthy() })