generated from nhcarrigan/template
a36c8e72a5
## Summary
- Add comprehensive try/catch error handling across all API routes, middleware, and the Hono global error handler, piping every unhandled error to the `@nhcarrigan/logger` service to prevent silent crashes and unhandled Promise rejections
- Add a `logError` utility on the frontend that forwards errors through the overridden `console.error` to the backend telemetry endpoint; apply it to every silent `catch {}` block in the game context, sound, notification, and clipboard utilities, and wrap the React tree in an `ErrorBoundary`
- Add Plausible analytics, Open Graph + Twitter Card meta tags, Tree-Nation widget, and Google Ads to `index.html`
- Make the game sidebar sticky with a `--resource-bar-height` CSS custom property offset so it stays viewport-height without overlapping the resource bar; reset sticky behaviour in the mobile responsive override
## Test plan
- [ ] Lint passes: `pnpm lint`
- [ ] Build passes: `pnpm build`
- [ ] Verify errors thrown in API routes appear in the logger service rather than crashing the process
- [ ] Verify frontend errors appear in the `/api/fe/error` backend log
- [ ] Verify Open Graph tags render correctly when sharing the URL
- [ ] Verify Plausible analytics fires on page load
- [ ] Verify Tree-Nation badge renders in the sidebar
- [ ] Verify sidebar stays fixed while the main content scrolls on desktop
- [ ] Verify mobile layout is unaffected
✨ This issue was created with help from Hikari~ 🌸
Reviewed-on: #44
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
/**
|
|
* @file Authentication middleware for validating JWT tokens.
|
|
* @copyright nhcarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
|
|
import { verifyToken } from "../services/jwt.js";
|
|
import { logger } from "../services/logger.js";
|
|
import type { HonoEnvironment } from "../types/hono.js";
|
|
import type { MiddlewareHandler } from "hono";
|
|
|
|
/**
|
|
* Validates the Authorization Bearer token on each request and attaches the discordId to context.
|
|
* @param context - The Hono context object.
|
|
* @param next - The next middleware handler.
|
|
* @returns A JSON error response if authentication fails, otherwise calls next.
|
|
*/
|
|
export const authMiddleware: MiddlewareHandler<HonoEnvironment> = async(
|
|
context,
|
|
next,
|
|
) => {
|
|
const authorization = context.req.header("Authorization");
|
|
|
|
if (authorization?.startsWith("Bearer ") !== true) {
|
|
return context.json(
|
|
{ error: "Missing or invalid Authorization header" },
|
|
401,
|
|
);
|
|
}
|
|
|
|
const token = authorization.slice(7);
|
|
|
|
try {
|
|
const payload = verifyToken(token);
|
|
context.set("discordId", payload.discordId);
|
|
} catch (error) {
|
|
void logger.error(
|
|
"auth_middleware",
|
|
error instanceof Error
|
|
? error
|
|
: new Error(String(error)),
|
|
);
|
|
return context.json({ error: "Invalid or expired token" }, 401);
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression -- Need the consistent return!
|
|
return await next();
|
|
};
|