generated from nhcarrigan/template
acda4c2fc4
- Add @types/node to API devDependencies
- Create HonoEnv type and apply to all routers + auth middleware for
proper context.get/set("discordId") typing
- Use conditional spreads for exactOptionalPropertyTypes dailyChallenges
in GameContext, tick engine, and prestige route
- Use conditional spread for optional signature in SaveRequest calls
- Add non-null assertions in shuffle/template index for noUncheckedIndexedAccess
- Cast GameState to never for Prisma InputJsonValue fields
- Exclude vite.config.ts from web tsconfig (it runs in Node context)
25 lines
712 B
TypeScript
25 lines
712 B
TypeScript
import type { MiddlewareHandler } from "hono";
|
|
import type { HonoEnv } from "../types/hono.js";
|
|
import { verifyToken } from "../services/jwt.js";
|
|
|
|
export const authMiddleware: MiddlewareHandler<HonoEnv> = async (context, next) => {
|
|
const authorization = context.req.header("Authorization");
|
|
|
|
if (!authorization?.startsWith("Bearer ")) {
|
|
context.status(401);
|
|
context.json({ error: "Missing or invalid Authorization header" });
|
|
return;
|
|
}
|
|
|
|
const token = authorization.slice(7);
|
|
|
|
try {
|
|
const payload = verifyToken(token);
|
|
context.set("discordId", payload.discordId);
|
|
await next();
|
|
} catch {
|
|
context.status(401);
|
|
context.json({ error: "Invalid or expired token" });
|
|
}
|
|
};
|