Files
Skori/server/src/middleware/auth.ts
T
Zed ce1db937f2
Build and Publish Docker Image / build-and-push-image (push) Successful in 1m48s
rework pwa cc
2026-07-11 02:01:08 +02:00

30 lines
670 B
TypeScript

import type { Request, Response, NextFunction } from "express";
import { verifyAccessToken } from "../utils/jwt.js";
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Express {
interface Request {
userId?: string;
}
}
}
export function requireAuth(
req: Request,
res: Response,
next: NextFunction,
): void {
const header = req.headers.authorization;
const token = header?.startsWith("Bearer ") ? header.slice(7) : null;
const userId = token ? verifyAccessToken(token) : null;
if (!userId) {
res.status(401).json({ error: "unauthorized" });
return;
}
req.userId = userId;
next();
}