Skip to content

Commit 6ad5d0d

Browse files
committed
update
1 parent 06a6130 commit 6ad5d0d

File tree

12 files changed

+494
-54
lines changed

12 files changed

+494
-54
lines changed

app/app.vue

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,46 @@
66
const { data: session } = await authClient.useSession(useFetch)
77
88
const items = computed<NavigationMenuItem[]>(() => {
9-
const baseItems: NavigationMenuItem[] = [
10-
{
11-
label: 'Dashboard',
12-
to: '/',
13-
icon: 'i-lucide-layout-dashboard',
14-
},
15-
{
16-
label: 'Rides',
17-
to: '/rides',
18-
icon: 'i-lucide-car',
19-
},
20-
{
21-
label: 'People',
22-
to: '/people',
23-
icon: 'i-lucide-users',
24-
},
25-
]
9+
const role = session.value?.user?.role
2610
27-
if (session.value?.user?.role === 'ADMIN') {
28-
baseItems.push({
29-
label: 'Admins',
30-
to: '/admin',
31-
icon: 'i-lucide-shield-check',
32-
})
11+
if (role === 'VOLUNTEER') {
12+
return [
13+
{
14+
label: 'Rides',
15+
to: '/rides',
16+
icon: 'i-lucide-car',
17+
},
18+
]
19+
}
20+
21+
if (role === 'ADMIN') {
22+
return [
23+
{
24+
label: 'Dashboard',
25+
to: '/',
26+
icon: 'i-lucide-layout-dashboard',
27+
},
28+
{
29+
label: 'Rides',
30+
to: '/rides',
31+
icon: 'i-lucide-car',
32+
},
33+
{
34+
label: 'People',
35+
to: '/people',
36+
icon: 'i-lucide-users',
37+
},
38+
]
3339
}
3440
35-
return baseItems
41+
return []
3642
})
3743
3844
async function handleLogout() {
3945
await authClient.signOut({
4046
fetchOptions: {
4147
onSuccess: () => {
42-
navigateTo('/auth')
48+
window.location.href = '/auth'
4349
},
4450
},
4551
})

app/middleware/auth.global.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@ export default defineNuxtRouteMiddleware(async (to) => {
77
if (to.path !== '/auth') {
88
return navigateTo('/auth')
99
}
10+
} else {
11+
// User is logged in
12+
const role = session.value.user.role
13+
if (role === 'VOLUNTEER') {
14+
// Volunteers can only access /rides and its sub-paths
15+
if (!to.path.startsWith('/rides')) {
16+
return navigateTo('/rides')
17+
}
18+
}
1019
}
1120
})

app/pages/auth.vue

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@
4545
otp: state.otp.join(''),
4646
})
4747
48-
const user = await $fetch(`/api/get/users/byEmail/${data.user.email}`)
49-
5048
if (error) {
5149
toast.add({ title: 'Error', description: error.message, color: 'error' })
5250
} else {
53-
await navigateTo('/', { external: true })
51+
const role = data.user.role
52+
if (role === 'VOLUNTEER') {
53+
await navigateTo('/rides', { external: true })
54+
} else {
55+
await navigateTo('/', { external: true })
56+
}
5457
}
5558
}
5659
}
@@ -78,9 +81,20 @@
7881
/>
7982
</UFormField>
8083

81-
<UButton loading-auto type="submit" class="w-full justify-center">
82-
{{ isEmailSent ? 'Login' : 'Send OTP' }}
83-
</UButton>
84+
<div class="flex gap-2">
85+
<UButton
86+
v-if="isEmailSent"
87+
color="neutral"
88+
variant="ghost"
89+
class="flex-1 justify-center"
90+
@click="isEmailSent = false"
91+
>
92+
Back
93+
</UButton>
94+
<UButton loading-auto type="submit" class="flex-1 justify-center">
95+
{{ isEmailSent ? 'Login' : 'Send OTP' }}
96+
</UButton>
97+
</div>
8498
</UForm>
8599
</UCard>
86100
</div>

0 commit comments

Comments
 (0)