feat(ST-350): add auth helpers — getSession, requireUser, requireWriter, requireProductAccess, requireProductWriter
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
fa34f680b3
commit
21befd0e5b
1 changed files with 34 additions and 0 deletions
34
lib/auth.ts
34
lib/auth.ts
|
|
@ -1,5 +1,39 @@
|
|||
import bcrypt from 'bcryptjs'
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { cookies } from 'next/headers'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { SessionData, sessionOptions } from '@/lib/session'
|
||||
import { getAccessibleProduct } from '@/lib/product-access'
|
||||
|
||||
export async function getSession() {
|
||||
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
||||
}
|
||||
|
||||
export async function requireUser() {
|
||||
const session = await getSession()
|
||||
if (!session.userId) throw new Error('Niet ingelogd')
|
||||
return session
|
||||
}
|
||||
|
||||
export async function requireWriter() {
|
||||
const session = await requireUser()
|
||||
if (session.isDemo) throw new Error('Niet beschikbaar in demo-modus')
|
||||
return session.userId
|
||||
}
|
||||
|
||||
export async function requireProductAccess(productId: string) {
|
||||
const session = await requireUser()
|
||||
const product = await getAccessibleProduct(productId, session.userId)
|
||||
if (!product) throw new Error('Product niet gevonden of geen toegang')
|
||||
return product
|
||||
}
|
||||
|
||||
export async function requireProductWriter(productId: string) {
|
||||
const userId = await requireWriter()
|
||||
const product = await getAccessibleProduct(productId, userId)
|
||||
if (!product) throw new Error('Product niet gevonden of geen toegang')
|
||||
return product
|
||||
}
|
||||
|
||||
export async function registerUser(username: string, password: string) {
|
||||
const existing = await prisma.user.findUnique({ where: { username } })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue