-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a89dd59
commit f37a505
Showing
4 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { AuthContext } from 'contexts/Auth'; | ||
|
||
export function useAuth() { | ||
const context = useContext(AuthContext); | ||
|
||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} |