mirror of
https://github.com/jorgev259/soc_site-astro.git
synced 2025-06-29 07:57:41 +00:00
Privileged header items
This commit is contained in:
parent
1631ce7bf6
commit
181cc33dd1
7 changed files with 88 additions and 27 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue