Skip to content

Commit 2278f6f

Browse files
authored
Merge pull request #173 from nottherealalanturing/navlinks-routeprotection
Navlinks routeprotection
2 parents 82ac977 + f6a1e33 commit 2278f6f

7 files changed

Lines changed: 99 additions & 19 deletions

File tree

mobile/app.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,26 @@
1010
"**/*"
1111
],
1212
"ios": {
13-
"supportsTablet": false
13+
"supportsTablet": false,
14+
"bundleIdentifier": "com.discoverly.app"
15+
},
16+
"android": {
17+
"package": "com.discoverly.app",
18+
"intentFilters": [
19+
{
20+
"action": "VIEW",
21+
"data": [
22+
{
23+
"scheme": "discoverly"
24+
}
25+
],
26+
"category": [
27+
"BROWSABLE",
28+
"DEFAULT"
29+
]
30+
}
31+
]
1432
},
15-
"android": {},
1633
"web": {
1734
"bundler": "metro"
1835
},

mobile/app/(auth)/_layout.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Redirect, Stack } from "expo-router"
2+
import { useAuthStore } from "../../src/store/useAuthStore"
3+
4+
export default function AuthLayout() {
5+
const token = useAuthStore((state) => state.token)
6+
7+
if (token) {
8+
return <Redirect href="/(tabs)/discover" />
9+
}
10+
11+
return (
12+
<Stack
13+
screenOptions={{
14+
headerShown: false,
15+
}}
16+
>
17+
<Stack.Screen name="login" />
18+
<Stack.Screen name="register" />
19+
</Stack>
20+
)
21+
}

mobile/app/(auth)/login.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { type LoginFormValues, loginSchema } from "../../src/validation/auth"
1010
export default function LoginScreen() {
1111
const router = useRouter()
1212
const setToken = useAuthStore((state) => state.setToken)
13+
1314
const {
1415
control,
1516
handleSubmit,
@@ -43,6 +44,7 @@ export default function LoginScreen() {
4344
<Typography variant="body" color={colors.muted}>
4445
Sign in to continue matching with dishes around you.
4546
</Typography>
47+
4648
<Controller
4749
control={control}
4850
name="email"
@@ -61,6 +63,7 @@ export default function LoginScreen() {
6163
/>
6264
)}
6365
/>
66+
6467
<Controller
6568
control={control}
6669
name="password"
@@ -79,6 +82,7 @@ export default function LoginScreen() {
7982
/>
8083
)}
8184
/>
85+
8286
<Button label="Sign In" loading={isSubmitting} onPress={handleSubmit(onSubmit)} />
8387
<Link href="/(auth)/register">Go To Register</Link>
8488
</View>

mobile/app/(auth)/register.tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { zodResolver } from "@hookform/resolvers/zod"
22
import { Link, useRouter } from "expo-router"
33
import { Controller, useForm } from "react-hook-form"
4-
import { ScrollView, View } from "react-native"
4+
import { ScrollView } from "react-native"
55
import { Button, Input, Typography } from "../../src/components"
66
import { useAuthStore } from "../../src/store/useAuthStore"
77
import { colors, spacing } from "../../src/theme/tokens"
@@ -10,6 +10,7 @@ import { type RegisterFormValues, registerSchema } from "../../src/validation/au
1010
export default function RegisterScreen() {
1111
const router = useRouter()
1212
const setToken = useAuthStore((state) => state.setToken)
13+
1314
const {
1415
control,
1516
handleSubmit,
@@ -46,6 +47,7 @@ export default function RegisterScreen() {
4647
<Typography variant="body" color={colors.muted}>
4748
Create your account to start discovering meals instantly.
4849
</Typography>
50+
4951
<Controller
5052
control={control}
5153
name="fullName"
@@ -61,6 +63,7 @@ export default function RegisterScreen() {
6163
/>
6264
)}
6365
/>
66+
6467
<Controller
6568
control={control}
6669
name="email"
@@ -79,24 +82,33 @@ export default function RegisterScreen() {
7982
/>
8083
)}
8184
/>
85+
8286
<Controller
8387
control={control}
8488
name="password"
8589
render={({ field: { onChange, onBlur, value } }) => (
86-
<Input
87-
label="Password"
88-
placeholder="Minimum 8 characters"
89-
secureTextEntry
90-
autoCapitalize="none"
91-
autoComplete="password-new"
92-
textContentType="newPassword"
93-
value={value}
94-
onChangeText={onChange}
95-
onBlur={onBlur}
96-
error={errors.password?.message}
97-
/>
90+
<>
91+
<Input
92+
label="Password"
93+
placeholder="Minimum 8 characters"
94+
secureTextEntry
95+
autoCapitalize="none"
96+
autoComplete="password-new"
97+
textContentType="newPassword"
98+
value={value}
99+
onChangeText={onChange}
100+
onBlur={onBlur}
101+
error={errors.password?.message}
102+
/>
103+
{!errors.password?.message ? (
104+
<Typography variant="caption" color={colors.muted}>
105+
Minimum 8 characters
106+
</Typography>
107+
) : null}
108+
</>
98109
)}
99110
/>
111+
100112
<Controller
101113
control={control}
102114
name="confirmPassword"
@@ -113,6 +125,7 @@ export default function RegisterScreen() {
113125
/>
114126
)}
115127
/>
128+
116129
<Button label="Sign Up" loading={isSubmitting} onPress={handleSubmit(onSubmit)} />
117130
<Link href="/(auth)/login">Back To Login</Link>
118131
</ScrollView>

mobile/app/(tabs)/_layout.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { Tabs } from "expo-router"
1+
import { Redirect, Tabs } from "expo-router"
22
import { useCartStore } from "../../src/store/useCartStore"
3+
import { useAuthStore } from "../../src/store/useAuthStore"
34

45
export default function TabsLayout() {
6+
const token = useAuthStore((state) => state.token)
57
const itemCount = useCartStore((state) => state.items.length)
68

9+
if (!token) {
10+
return <Redirect href="/(auth)/login" />
11+
}
12+
713
return (
814
<Tabs>
915
<Tabs.Screen name="discover" options={{ title: "Discover" }} />

mobile/app/(tabs)/profile.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
import { Text, View } from "react-native"
1+
import { useRouter } from "expo-router"
2+
import { Pressable, Text, View } from "react-native"
3+
import { useAuthStore } from "../../src/store/useAuthStore"
24

35
export default function ProfileScreen() {
6+
const router = useRouter()
7+
const logout = useAuthStore((state) => state.logout)
8+
9+
const onLogout = () => {
10+
logout()
11+
router.replace("/(auth)/login")
12+
}
13+
414
return (
5-
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
15+
<View style={{ flex: 1, alignItems: "center", justifyContent: "center", gap: 12 }}>
616
<Text>User Profile & Wallet.</Text>
17+
<Pressable
18+
onPress={onLogout}
19+
style={{ backgroundColor: "#E53935", borderRadius: 10, paddingHorizontal: 14, paddingVertical: 10 }}
20+
>
21+
<Text style={{ color: "#fff", fontWeight: "700" }}>Logout</Text>
22+
</Pressable>
723
</View>
824
)
925
}

mobile/app/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Redirect } from "expo-router"
2+
import { useAuthStore } from "../src/store/useAuthStore"
23

34
export default function IndexScreen() {
4-
return <Redirect href="/(auth)/login" />
5+
const token = useAuthStore((state) => state.token)
6+
7+
return <Redirect href={token ? "/(tabs)/discover" : "/(auth)/login"} />
58
}

0 commit comments

Comments
 (0)