File tree Expand file tree Collapse file tree 3 files changed +9
-14
lines changed Expand file tree Collapse file tree 3 files changed +9
-14
lines changed Original file line number Diff line number Diff line change 1
1
<script setup lang="ts">
2
2
import type { AllClubs } from ' @@/types/api/user/all_clubs'
3
- import type { getRoleResponse } from ' ~~/utils/user-roles'
4
3
import { useClerk , useUser } from ' vue-clerk'
5
- import { isAdmin } from ' ~~/utils/user-roles'
6
4
7
5
const { user } = useUser ()
8
6
const config = useRuntimeConfig ()
@@ -23,11 +21,11 @@ const { data: clubs, suspense: __s1 } = useQuery<AllClubs>({
23
21
await __s1 ()
24
22
25
23
const isUserAdmin = ref (false )
26
- const { data : roleData, suspense : __s2 } = useQuery <getRoleResponse >({
24
+ const { data : roleData, suspense : __s2 } = useQuery <any >({
27
25
queryKey: [' /api/user/check-role' ],
28
26
})
29
27
await __s2 ()
30
- isUserAdmin .value = isAdmin ( roleData .value )
28
+ isUserAdmin .value = roleData .value ?. success === true && roleData . value . role === ' ADMIN '
31
29
32
30
if (clubs .value ) {
33
31
useState (' isEnspireLoading' ).value = false
Original file line number Diff line number Diff line change 1
- import { Roles } from '@prisma/client'
2
1
import { until } from '@vueuse/core'
3
2
import { useClerk , useClerkProvider } from 'vue-clerk'
4
- import { getRole , isAdmin } from '~~/utils/user-roles'
3
+ import { getRole } from '~~/utils/user-roles'
5
4
6
5
export default defineNuxtRouteMiddleware ( async ( ) => {
7
6
// Modified from auth.ts
@@ -18,7 +17,7 @@ export default defineNuxtRouteMiddleware(async () => {
18
17
if ( clerk . loaded && clerk . user ?. id == null )
19
18
return navigateTo ( '/sign-in' )
20
19
const response = await $fetch ( '/api/user/check-role' )
21
- if ( ! isAdmin ( response ) )
20
+ if ( ! ( response ?. success === true && response . role === 'ADMIN' ) )
22
21
return abortNavigation ( )
23
22
}
24
23
@@ -27,7 +26,7 @@ export default defineNuxtRouteMiddleware(async () => {
27
26
if ( id == null )
28
27
return navigateTo ( '/sign-in' )
29
28
const response = await getRole ( id )
30
- if ( ! isAdmin ( response ) )
29
+ if ( ! ( response ?. success === true && response . role === 'ADMIN' ) )
31
30
return abortNavigation ( )
32
31
}
33
32
} )
Original file line number Diff line number Diff line change 1
- import type { $Enums } from '@prisma/client'
2
1
import { PrismaClient , Roles } from '@prisma/client'
3
2
4
- const prisma = new PrismaClient ( )
5
-
6
3
export interface getRoleResponse {
7
4
success : boolean
8
- role : $Enums . Roles | null
5
+ role : string | null
9
6
error ?: string
10
7
}
11
8
12
- export async function getRole ( clerkId : string ) {
9
+ export async function getRole ( clerkId : string ) : Promise < getRoleResponse > {
13
10
try {
11
+ const prisma = new PrismaClient ( )
14
12
const user = await prisma . user . findFirstOrThrow ( {
15
13
where : {
16
14
clerkUserId : clerkId ,
@@ -30,7 +28,7 @@ export async function getRole(clerkId: string) {
30
28
}
31
29
}
32
30
33
- export function isRole ( roleData : getRoleResponse | undefined , theRole : $Enums . Roles ) {
31
+ export function isRole ( roleData : getRoleResponse | undefined , theRole : string ) {
34
32
return roleData ?. success === true && roleData . role === theRole
35
33
}
36
34
You can’t perform that action at this time.
0 commit comments