initial commit

This commit is contained in:
yiling
2026-05-19 16:05:09 +08:00
commit 9cad96210c
370 changed files with 131609 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
import type { Prisma } from '@/generated/prisma/client'
import { auth } from '@/services/auth/auth'
import { prisma } from '@/db'
import { createMiddleware } from 'hono/factory'
type User = Prisma.UserGetPayload<Record<string, never>>
export const authMiddleware = createMiddleware<{
Variables: {
user: User
}
}>(async (c, next) => {
const session = await auth.api.getSession({
headers: c.req.raw.headers,
})
if (!session) {
return c.json({ error: 'Unauthorized' }, 401)
}
try {
const user = await prisma.user.findUnique({
where: { id: session.user.id },
})
if (!user) {
return c.json({ error: 'User not found' }, 401)
}
c.set('user', user)
await next()
} catch {
return c.json({ error: 'Authentication failed' }, 401)
}
})