Skip to content

Commit f7d2863

Browse files
author
Manu Genia
committed
feat: use protected routes
1 parent d3d1dc3 commit f7d2863

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

src/app/_layout.tsx

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { KeyboardProvider } from 'react-native-keyboard-controller';
1414

1515
import { APIProvider } from '@/api';
1616
import interceptors from '@/api/common/interceptors';
17-
import { AuthProvider } from '@/components/providers/auth';
18-
import { hydrateAuth, loadSelectedTheme } from '@/lib';
17+
import { AuthProvider, useAuth } from '@/components/providers/auth';
18+
import { hydrateAuth, loadSelectedTheme, useIsFirstTime } from '@/lib';
1919
import { useThemeConfig } from '@/lib/use-theme-config';
2020

2121
export { ErrorBoundary } from 'expo-router';
@@ -35,36 +35,65 @@ SplashScreen.setOptions({
3535
fade: true,
3636
});
3737

38-
export default function RootLayout() {
38+
function GuardedStack() {
39+
const { isAuthenticated } = useAuth();
3940
const { t } = useTranslation();
41+
const [isFirstTime] = useIsFirstTime();
42+
4043
return (
41-
<Providers>
42-
<Stack>
43-
<Stack.Screen name="(app)" options={{ headerShown: false }} />
44+
<Stack>
45+
<Stack.Protected guard={isFirstTime}>
4446
<Stack.Screen name="onboarding" options={{ headerShown: false }} />
45-
<Stack.Screen name="forgot-password" />
47+
</Stack.Protected>
48+
49+
<Stack.Protected guard={isAuthenticated}>
50+
<Stack.Screen name="(app)" options={{ headerShown: false }} />
4651
<Stack.Screen
4752
name="update-password"
4853
options={{
4954
title: t('updatePassword.title'),
5055
}}
5156
/>
57+
</Stack.Protected>
58+
59+
<Stack.Protected guard={!isAuthenticated}>
5260
<Stack.Screen name="sign-in" options={{ headerShown: false }} />
5361
<Stack.Screen name="sign-up" />
54-
<Stack.Screen
55-
name="www"
56-
options={{
57-
presentation: 'modal',
58-
title: '', // Title will be overridden by the screen itself
59-
}}
60-
/>
61-
</Stack>
62+
<Stack.Screen name="forgot-password" />
63+
</Stack.Protected>
64+
65+
<Stack.Screen
66+
name="www"
67+
options={{
68+
presentation: 'modal',
69+
title: '',
70+
}}
71+
/>
72+
</Stack>
73+
);
74+
}
75+
76+
export default function RootLayout() {
77+
return (
78+
<Providers>
79+
<RouterContent />
6280
</Providers>
6381
);
6482
}
6583

66-
function Providers({ children }: { children: React.ReactNode }) {
84+
function RouterContent() {
85+
const { ready } = useAuth();
86+
87+
if (!ready) {
88+
return <Stack />;
89+
}
90+
91+
return <GuardedStack />;
92+
}
93+
94+
function Providers({ children }: Readonly<{ children: React.ReactNode }>) {
6795
const theme = useThemeConfig();
96+
6897
return (
6998
<GestureHandlerRootView
7099
style={styles.container}

src/app/sign-in.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useRouter } from 'expo-router';
21
import React from 'react';
32
import { showMessage } from 'react-native-flash-message';
43

@@ -7,12 +6,7 @@ import { LoginForm, type LoginFormProps } from '@/components/login-form';
76
import { FocusAwareStatusBar } from '@/components/ui';
87

98
export default function Login() {
10-
const router = useRouter();
11-
129
const { mutate: login, isPending } = useLogin({
13-
onSuccess: () => {
14-
router.push('/');
15-
},
1610
onError: (error) => showMessage({ message: error.message, type: 'danger' }),
1711
});
1812

src/app/sign-up.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useRouter } from 'expo-router';
21
import React from 'react';
32
import { showMessage } from 'react-native-flash-message';
43

@@ -8,12 +7,7 @@ import { SignUpForm } from '@/components/sign-up-form';
87
import { FocusAwareStatusBar } from '@/components/ui';
98

109
export default function SignIn() {
11-
const router = useRouter();
12-
1310
const { mutate: signUp, isPending } = useSignUp({
14-
onSuccess: () => {
15-
router.push('/');
16-
},
1711
onError: (error) => showMessage({ message: error.message, type: 'danger' }),
1812
});
1913

0 commit comments

Comments
 (0)