diff --git a/src/hooks/useAuth.tsx b/src/hooks/useAuth.tsx new file mode 100644 index 0000000..087e4e6 --- /dev/null +++ b/src/hooks/useAuth.tsx @@ -0,0 +1,9 @@ +import { useContext } from 'react'; + +import { AuthContext } from 'contexts/Auth'; + +export function useAuth() { + const context = useContext(AuthContext); + + return context; +} diff --git a/src/hooks/useForgetPassword.tsx b/src/hooks/useForgetPassword.tsx new file mode 100644 index 0000000..6c7894b --- /dev/null +++ b/src/hooks/useForgetPassword.tsx @@ -0,0 +1,61 @@ +import { useNavigate } from 'react-router-dom'; + +import { useFormik } from 'formik'; +import { forgetPasswordSchema, resetPasswordSchema } from 'validations/ForgetPassword'; + +import { useToast } from 'contexts/Toast'; + +import { supabase } from 'services/supabase'; + +export function useForgetPassword() { + const navigate = useNavigate(); + const { toast } = useToast(); + + const formikForgetPassword = useFormik({ + initialValues: { + email: '', + }, + validationSchema: forgetPasswordSchema, + onSubmit: async (values) => { + toast.loading('Enviando email...', { id: 'toast' }); + const { error } = await supabase.auth.api.resetPasswordForEmail(values.email, { + redirectTo: 'http://localhost:3000/reset-password', + }); + + if (error) { + toast.error(error.message, { id: 'toast' }); + return; + } + + toast.success('Email enviado com sucesso!', { id: 'toast' }); + navigate('/'); + }, + }); + + const formikResetPassword = useFormik({ + initialValues: { + senha: '', + confirmarSenha: '', + }, + validationSchema: resetPasswordSchema, + onSubmit: async (values) => { + toast.loading('Alterando senha...', { id: 'toast' }); + const { error } = await supabase.auth.update({ + password: values.senha, + }); + + if (error) { + toast.error(error.message, { id: 'toast' }); + return; + } + + toast.success('Senha alterada com sucesso!', { id: 'toast' }); + navigate('/'); + }, + }); + + return { + formikForgetPassword, + formikResetPassword, + }; +} diff --git a/src/hooks/useProfile.tsx b/src/hooks/useProfile.tsx new file mode 100644 index 0000000..8b8d1ce --- /dev/null +++ b/src/hooks/useProfile.tsx @@ -0,0 +1,58 @@ +import { useEffect, useState } from 'react'; + +import { useFormik } from 'formik'; +import { profileSchema } from 'validations/Profile'; + +import { useToast } from 'contexts/Toast'; + +import { useAuth } from 'hooks/useAuth'; + +import { supabase } from 'services/supabase'; + +export function useProfile() { + const { toast } = useToast(); + + const { user, setUser } = useAuth(); + const [loading, setLoading] = useState(false); + + const formikProfile = useFormik({ + initialValues: { + avatar: '', + nome: '', + email: '', + }, + validationSchema: profileSchema, + onSubmit: async (values) => { + setLoading(true); + const { user: userData, error } = await supabase.auth.update({ + email: values.email, + data: { name: values.nome }, + }); + + const { error: errorAvatar } = await supabase.rpc('upsert_profile_photo', { + p_user_id: user?.id, + p_src: values.avatar, + }); + + if (errorAvatar || error) { + toast.error('Não foi possível atualizar perfil', { id: 'toast' }); + setLoading(false); + return; + } + + toast.success('Perfil atualizado com sucesso', { id: 'toast' }); + setLoading(false); + setUser(userData); + }, + }); + + useEffect(() => { + formikProfile.setFieldValue('nome', user?.user_metadata.name); + formikProfile.setFieldValue('email', user?.email); + }, [user]); + + return { + formikProfile, + loading, + }; +} diff --git a/src/hooks/useRegister.tsx b/src/hooks/useRegister.tsx new file mode 100644 index 0000000..3b99746 --- /dev/null +++ b/src/hooks/useRegister.tsx @@ -0,0 +1,95 @@ +import { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; + +import { useFormik } from 'formik'; +import { registerSchema } from 'validations/Register'; + +import { useToast } from 'contexts/Toast'; + +import { useAuth } from 'hooks/useAuth'; + +import { supabase } from 'services/supabase'; + +export function useRegister() { + const navigate = useNavigate(); + const { ocupacao } = useAuth(); + const { toast } = useToast(); + + const [loading, setLoading] = useState(false); + const [status, setStatus] = useState<'success' | 'error' | ''>(''); + + const formikRegister = useFormik({ + initialValues: { + nome: '', + email: '', + senha: '', + confirmarSenha: '', + ocupacao: '', + }, + validationSchema: registerSchema, + onSubmit: async (values) => { + try { + setLoading(true); + + const { error } = await supabase.auth.signUp( + { + email: values.email, + password: values.senha, + }, + { + data: { + name: values.nome, + ocupacao: ocupacao, + }, + }, + ); + + if (error) { + toast.error(error.message, { id: 'toast' }); + setStatus('error'); + setLoading(false); + return; + } + + setStatus('success'); + setLoading(false); + } catch (error: any) { + toast.error(error?.message, { id: 'toast' }); + setStatus('error'); + setLoading(false); + } finally { + setTimeout(function () { + navigate('/'); + }, 4000); + } + }, + }); + + const formikLoginGoogle = useFormik({ + initialValues: { + ocupacao: '', + }, + validationSchema: ocupacao ? '' : registerSchema, + onSubmit: async (values) => { + const { error } = await supabase.auth.update({ + data: { ocupacao: values.ocupacao }, + }); + + if (error) { + toast.error(error.message, { id: 'toast' }); + return; + } + + toast.success('Função adicionada com sucesso!', { id: 'toast' }); + + window.location.reload(); + }, + }); + + return { + formikRegister, + formikLoginGoogle, + loading, + status, + }; +}