UNPKG

tezx

Version:

TezX is a modern, ultra-lightweight, and high-performance JavaScript framework built specifically for Bun. It provides a minimal yet powerful API, seamless environment management, and a high-concurrency HTTP engine for building fast, scalable web applicat

31 lines (30 loc) 1.11 kB
export const bearerAuth = (options) => { const { validate, realm = "API", onUnauthorized = (ctx, error) => { ctx.status(401); ctx.setHeader("WWW-Authenticate", `Bearer realm="${realm}"`); return ctx.json({ error: error?.message || "Unauthorized" }); }, } = options; return async (ctx, next) => { const auth = ctx.req.header("authorization"); if (!auth || !auth.startsWith("Bearer ")) { return onUnauthorized(ctx, new Error("Bearer token required")); } const token = auth.slice(7).trim(); if (!token) { return onUnauthorized(ctx, new Error("Empty token")); } try { const valid = await validate(token, ctx); if (!valid) { return onUnauthorized(ctx, new Error("Invalid or expired token")); } ctx.token = token; await next(); } catch (err) { let error = err instanceof Error ? err : new Error(err); return onUnauthorized(ctx, error); } }; }; export default bearerAuth;