From e24a4f3fc18dea50a559d91faab0148b31e56e4a Mon Sep 17 00:00:00 2001 From: Aditya Pawar <34043950+adityapawar1@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:52:02 -0700 Subject: [PATCH] [auth] Add auth context and useSession hook (#17) * Add AuthContext * Add useSession hook in authentication components * Run formatter * Fix tsc errors --------- Co-authored-by: Aditya Pawar --- src/app/_layout.tsx | 13 +++--- src/app/auth/onboarding.tsx | 22 ++-------- src/app/auth/signup.tsx | 22 ++-------- src/components/Account.tsx | 8 ++-- src/components/Login.tsx | 23 +++++----- src/utils/AuthContext.tsx | 86 +++++++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 57 deletions(-) create mode 100644 src/utils/AuthContext.tsx diff --git a/src/app/_layout.tsx b/src/app/_layout.tsx index 9f24129b..a96faef8 100644 --- a/src/app/_layout.tsx +++ b/src/app/_layout.tsx @@ -1,12 +1,15 @@ import { Stack } from 'expo-router'; +import { AuthContextProvider } from '../utils/AuthContext'; function StackLayout() { return ( - - - - - + + + + + + + ); } diff --git a/src/app/auth/onboarding.tsx b/src/app/auth/onboarding.tsx index 2389ec8d..2f0f643d 100644 --- a/src/app/auth/onboarding.tsx +++ b/src/app/auth/onboarding.tsx @@ -1,30 +1,14 @@ -import { useState, useEffect } from 'react'; import { View } from 'react-native'; -import { Session } from '@supabase/supabase-js'; -import supabase from '../../utils/supabase'; import Login from '../../components/Login'; import Account from '../../components/Account'; +import { useSession } from '../../utils/AuthContext'; function OnboardingScreen() { - const [session, setSession] = useState(null); - - useEffect(() => { - supabase.auth.getSession().then(({ data: { session: newSession } }) => { - setSession(newSession); - }); - - supabase.auth.onAuthStateChange((_event, newSession) => { - setSession(newSession); - }); - }, []); + const { session } = useSession(); return ( - {session && session.user ? ( - - ) : ( - - )} + {session && session.user ? : } ); } diff --git a/src/app/auth/signup.tsx b/src/app/auth/signup.tsx index 4dcdbe26..8072d3c9 100644 --- a/src/app/auth/signup.tsx +++ b/src/app/auth/signup.tsx @@ -1,30 +1,14 @@ -import { Session } from '@supabase/supabase-js'; -import { useEffect, useState } from 'react'; import { View } from 'react-native'; import Account from '../../components/Account'; import Login from '../../components/Login'; -import supabase from '../../utils/supabase'; +import { useSession } from '../../utils/AuthContext'; function SignUpScreen() { - const [session, setSession] = useState(null); - - useEffect(() => { - supabase.auth.getSession().then(({ data: { session: newSession } }) => { - setSession(newSession); - }); - - supabase.auth.onAuthStateChange((_event, newSession) => { - setSession(newSession); - }); - }, []); + const { session } = useSession(); return ( - {session && session.user ? ( - - ) : ( - - )} + {session && session.user ? : } ); } diff --git a/src/components/Account.tsx b/src/components/Account.tsx index 59c14d67..ebe214f1 100644 --- a/src/components/Account.tsx +++ b/src/components/Account.tsx @@ -1,10 +1,10 @@ import { useState, useEffect } from 'react'; import { StyleSheet, View, Alert, ScrollView, Platform } from 'react-native'; import { Button, Input } from 'react-native-elements'; -import { Session } from '@supabase/supabase-js'; import DateTimePicker from '@react-native-community/datetimepicker'; import supabase from '../utils/supabase'; import UserStringInput from './UserStringInput'; +import { useSession } from '../utils/AuthContext'; const styles = StyleSheet.create({ container: { @@ -21,7 +21,9 @@ const styles = StyleSheet.create({ }, }); -export default function Account({ session }: { session: Session }) { +export default function Account() { + const { session, signOut } = useSession(); + const [loading, setLoading] = useState(true); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); @@ -160,7 +162,7 @@ export default function Account({ session }: { session: Session }) { /> -