Privileged header items

This commit is contained in:
Jorge Vargas 2025-02-11 00:13:15 -06:00
parent 1631ce7bf6
commit 181cc33dd1
7 changed files with 88 additions and 27 deletions

View file

@ -1,6 +1,9 @@
import { auth } from 'auth'
import { defineMiddleware } from 'astro:middleware'
import PAGES from 'utils/pages.json'
import prismaClient from 'utils/prisma-client'
export const onRequest = defineMiddleware(async (context, next) => {
const isAuthed = await auth.api.getSession({
headers: context.request.headers
@ -9,9 +12,21 @@ export const onRequest = defineMiddleware(async (context, next) => {
if (isAuthed) {
context.locals.user = isAuthed.user
context.locals.session = isAuthed.session
const user = await prismaClient.users.findUnique({
select: { roleList: { select: { roles: { select: { permissions: true } } } } },
where: { id: isAuthed.user.id }
})
const permissions = (user?.roleList.map((r) => r.roles.permissions).flat() as string[]) ?? []
const pages = PAGES.filter((p) => p.perms.some((r) => permissions.includes(r))).map((p) => p.url)
context.locals.permissions = permissions
context.locals.pages = pages
} else {
context.locals.user = null
context.locals.session = null
context.locals.permissions = []
context.locals.pages = []
}
return next()