Skip to content

Commit 4edecb1

Browse files
committed
3️⃣ 3.4 Protected Route
1 parent 16013d5 commit 4edecb1

File tree

4 files changed

+49
-34
lines changed

4 files changed

+49
-34
lines changed

apps/expo/app/(tabs).tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { StatusBar } from 'expo-status-bar'
33
import { Tabs } from 'expo-router'
44
import { useDripsyTheme } from 'dripsy'
55
import { Ionicons } from '@expo/vector-icons'
6+
import { useAuth } from 'app/features/auth/context'
67

78
function MyTabs() {
89
const { colors } = useDripsyTheme().theme
10+
const auth = useAuth()
11+
912
return (
1013
<Tabs
1114
screenOptions={{
@@ -45,6 +48,7 @@ function MyTabs() {
4548
/>
4649
)
4750
},
51+
tabBarButton: auth ? undefined : () => null,
4852
}}
4953
/>
5054
<Tabs.Screen

packages/app/features/auth/firebase/init.web.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ let auth: ReturnType<typeof initializeAuth>
1313

1414
if (typeof window !== 'undefined') {
1515
const firebaseApp = initializeApp({
16-
// TODO REPLACE THIS WITH YOUR AUTH CONFIG
1716
apiKey: 'AIzaSyAQZ1A-bJMQqjdzNQhRPkbA7swEFnwUS_w',
1817
authDomain: 'solito-example.firebaseapp.com',
1918
projectId: 'solito-example',
Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { View, Image, ScrollView, Text } from 'dripsy'
22
import { Link } from 'solito/link'
33
import { Fragment } from 'react'
4+
import { AuthGate } from '../auth/gate'
45

56
const users = [
67
{
@@ -27,42 +28,46 @@ const users = [
2728

2829
export default function UsersListScreen() {
2930
return (
30-
<ScrollView sx={{ flex: 1, bg: '$background' }}>
31-
<View sx={{ p: '$3', width: '100%', maxWidth: 800, alignSelf: 'center' }}>
32-
{users.map((user) => {
33-
return (
34-
<Fragment key={user.id}>
35-
<Link href={`/users/${user.id}`}>
36-
<View
37-
sx={{
38-
mb: '$3',
39-
flexDirection: 'row',
40-
}}
41-
>
42-
<Image
43-
source={{ uri: user.avatar, cache: 'force-cache' }}
31+
<AuthGate>
32+
<ScrollView sx={{ flex: 1, bg: '$background' }}>
33+
<View
34+
sx={{ p: '$3', width: '100%', maxWidth: 800, alignSelf: 'center' }}
35+
>
36+
{users.map((user) => {
37+
return (
38+
<Fragment key={user.id}>
39+
<Link href={`/users/${user.id}`}>
40+
<View
4441
sx={{
45-
size: 50,
46-
borderRadius: '$rounded',
47-
mr: '$3',
42+
mb: '$3',
43+
flexDirection: 'row',
4844
}}
49-
{...{
50-
alt: `${user.id}'s avatar`,
51-
accessibilityLabel: `${user.id}'s avatar`,
52-
}}
53-
/>
45+
>
46+
<Image
47+
source={{ uri: user.avatar, cache: 'force-cache' }}
48+
sx={{
49+
size: 50,
50+
borderRadius: '$rounded',
51+
mr: '$3',
52+
}}
53+
{...{
54+
alt: `${user.id}'s avatar`,
55+
accessibilityLabel: `${user.id}'s avatar`,
56+
}}
57+
/>
5458

55-
<View sx={{ flex: 1, justifyContent: 'center' }}>
56-
<Text sx={{ fontWeight: '600', fontSize: 14 }}>
57-
{user.id}
58-
</Text>
59+
<View sx={{ flex: 1, justifyContent: 'center' }}>
60+
<Text sx={{ fontWeight: '600', fontSize: 14 }}>
61+
{user.id}
62+
</Text>
63+
</View>
5964
</View>
60-
</View>
61-
</Link>
62-
</Fragment>
63-
)
64-
})}
65-
</View>
66-
</ScrollView>
65+
</Link>
66+
</Fragment>
67+
)
68+
})}
69+
</View>
70+
</ScrollView>
71+
</AuthGate>
6772
)
6873
}

packages/app/layout/web/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useAuth } from 'app/features/auth/context'
12
import { View, Text, A } from 'dripsy'
23
import { useRouter } from 'next/router'
34
import { Fragment } from 'react'
@@ -7,6 +8,7 @@ const tabs: Array<{
78
pathname: string
89
isActive(pathname: string): boolean
910
name: string
11+
protected?: boolean
1012
}> = [
1113
{
1214
pathname: '/',
@@ -17,6 +19,7 @@ const tabs: Array<{
1719
pathname: '/users',
1820
isActive: (pathname) => pathname.startsWith('/users'),
1921
name: 'Users',
22+
protected: true,
2023
},
2124
{
2225
pathname: '/account',
@@ -30,6 +33,7 @@ const height = 34
3033
// this will only run on Web
3134
export function WebLayout({ children }: { children: React.ReactNode }) {
3235
const { pathname } = useRouter()
36+
const auth = useAuth()
3337
return (
3438
<>
3539
<View
@@ -54,6 +58,9 @@ export function WebLayout({ children }: { children: React.ReactNode }) {
5458
>
5559
{tabs.map((tab) => {
5660
const active = tab.isActive(pathname)
61+
if (tab.protected && !auth) {
62+
return null
63+
}
5764
return (
5865
<Fragment key={tab.pathname}>
5966
<Link href={tab.pathname}>

0 commit comments

Comments
 (0)