/**
* Shared auth helpers for route handlers.
*/
import { UnauthorizedError, ForbiddenError } from '../errors.js'
export async function requireAuth (req, server, action, resource) {
const { authorized, auth, reason } = await server.authManager.authorizeRequest(req, action, resource)
if (!auth) throw new UnauthorizedError('Authentication required')
if (!authorized) throw new ForbiddenError(reason, action)
return auth
}
export async function optionalAuth (req, server) {
try {
return await server.authManager.authenticate(req)
} catch {
return null
}
}