Skip to content

Commit

Permalink
refactor: refactored redirect effect in main layout
Browse files Browse the repository at this point in the history
  • Loading branch information
usotsukki committed Oct 20, 2024
1 parent 346d942 commit 022e062
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app/(tabs)/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Redirect } from 'expo-router'

export default () => {
return <Redirect href="/(demo)" />
return <Redirect href="/(home)/Home" />
}
25 changes: 14 additions & 11 deletions src/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import auth from '@react-native-firebase/auth'
import { GoogleSignin } from '@react-native-google-signin/google-signin'
import * as Sentry from '@sentry/react-native'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { isRunningInExpoGo } from 'expo'
import { Slot, useNavigationContainerRef, useRouter } from 'expo-router'
import { Slot, useNavigationContainerRef, useRouter, useSegments } from 'expo-router'
import * as SplashScreen from 'expo-splash-screen'
import { useEffect } from 'react'
import { SafeAreaProvider } from 'react-native-safe-area-context'
Expand All @@ -12,6 +11,7 @@ import { NetInfoToast, Toast } from '@app/components'
import { GOOGLE_WEB_CLIENT_ID, IS_PROD, SENTRY_DEBUG, SENTRY_DSN } from '@app/env'
import { useLoadFonts } from '@app/hooks'
import { useStorageDevTools } from '@app/storage'
import { useStore } from '@app/store'
import '@app/theme'
import '@app/translations'

Expand All @@ -37,28 +37,31 @@ const RootLayout = () => {
const ref = useNavigationContainerRef()
const { fontsFinishedLoading } = useLoadFonts()
const router = useRouter()
const segments = useSegments()
const queryClient = new QueryClient()
const user = useStore(state => state.auth.user)
const authRequired = segments[0] === '(tabs)'

useEffect(() => {
if (fontsFinishedLoading) {
const unsubscribe = auth().onAuthStateChanged(user =>
// setTimeout used to ensure that all synchronous state updates and rendering are executed first
setTimeout(() => {
router.replace(user ? '/(tabs)/Home' : '/(auth)/Welcome')
}),
)

SplashScreen.hideAsync()
routingInstrumentation.registerNavigationContainer(ref)
GoogleSignin.configure({
webClientId: GOOGLE_WEB_CLIENT_ID,
})
GoogleSignin.hasPlayServices()

return unsubscribe
}
}, [fontsFinishedLoading])

useEffect(() => {
if (authRequired && !user) {
router.replace('/(auth)/Welcome')
}
if (!authRequired && user) {
router.replace('/(tabs)/Home')
}
}, [user, authRequired])

useStorageDevTools()

return (
Expand Down

0 comments on commit 022e062

Please sign in to comment.