From ca28bb4bfb9b33ee875fba6c881a3076144ab3b1 Mon Sep 17 00:00:00 2001 From: Aditya Pawar Date: Sun, 19 Nov 2023 15:43:13 -0800 Subject: [PATCH] Create auth.tsx to use dispatch to control state --- src/app/auth/forgotPassword.tsx | 28 ++++--- src/app/auth/login.tsx | 27 +++---- src/app/auth/signup.tsx | 15 ++-- src/app/auth/verify.tsx | 20 ++--- src/queries/auth.tsx | 90 ++++++++++++++++++++++ src/utils/AuthContext.tsx | 128 +++----------------------------- 6 files changed, 132 insertions(+), 176 deletions(-) create mode 100644 src/queries/auth.tsx diff --git a/src/app/auth/forgotPassword.tsx b/src/app/auth/forgotPassword.tsx index 9d67d625..8737828b 100644 --- a/src/app/auth/forgotPassword.tsx +++ b/src/app/auth/forgotPassword.tsx @@ -5,17 +5,22 @@ import { Button, Input } from 'react-native-elements'; import globalStyles from '../../styles/globalStyles'; import { useSession } from '../../utils/AuthContext'; +import { + resetPassword, + signOut, + updateUser, + verifyOtp, +} from '../../queries/auth'; function ForgotPasswordScreen() { const { dispatch, isLoading } = useSession(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); - const [loading, setLoading] = useState(false); const [verificationCode, setCode] = useState(''); const [changingPassword, setChangingPassword] = useState(false); const sendResetEmail = async () => { - const { error } = await resetPassword(email); + const { error } = await resetPassword(dispatch, email); if (error) Alert.alert('Could not send a reset password email. Please try again.'); else @@ -24,10 +29,8 @@ function ForgotPasswordScreen() { ); }; const verifyCode = async () => { - setLoading(true); - if (email && verificationCode) { - const { error } = await verifyOtp(email, verificationCode); + const { error } = await verifyOtp(dispatch, email, verificationCode); if (error) { Alert.alert(error.message); @@ -40,23 +43,18 @@ function ForgotPasswordScreen() { } else { Alert.alert(`Please sign up again.`); } - - setLoading(false); }; const changePassword = async () => { - setLoading(true); - const { error } = await updateUser({ password }); + const { error } = await updateUser(dispatch, { password }); if (error) { console.error(error); Alert.alert('Updating password failed'); } else { - await signOut(); + await signOut(dispatch); router.replace('/auth/login'); } - - setLoading(false); }; return ( @@ -72,7 +70,7 @@ function ForgotPasswordScreen() { /> -