pping / src /middlewares /api-key-authentication.ts
understanding's picture
Upload 25 files
656c481 verified
raw
history blame contribute delete
626 Bytes
import { Request, Response, NextFunction } from "express";
const authentication = (req: Request, res: Response, next: NextFunction) => {
const authHeader = req.headers["authorization"];
if (!authHeader) {
return res.status(401).json({ message: "Missing authorization header" });
}
const apiKey = authHeader.split("Bearer ").at(-1);
if (!apiKey) {
return res.status(401).json({ message: "Invalid authorization format" });
}
if (process.env.API_KEY && apiKey === process.env.API_KEY) {
next();
} else {
res.status(401).json({ message: "Unauthorized" });
}
};
export default authentication;