Initial commit
This commit is contained in:
30
dist/shared/middleware/auth.js
vendored
Normal file
30
dist/shared/middleware/auth.js
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
import { ApiError } from "../errors";
|
||||
import { env } from "../../config/env";
|
||||
function extractAdminToken(req) {
|
||||
const raw = req.header("authorization") || "";
|
||||
if (raw.startsWith("Bearer ")) {
|
||||
return raw.slice(7);
|
||||
}
|
||||
return raw || req.header("x-admin-token") || "";
|
||||
}
|
||||
export function requireAdminToken(req, _res, next) {
|
||||
const token = extractAdminToken(req);
|
||||
if (!token) {
|
||||
return next(new ApiError("UNAUTHORIZED", "Missing admin bearer token", 401));
|
||||
}
|
||||
if (token !== env.ADMIN_TOKEN) {
|
||||
return next(new ApiError("UNAUTHORIZED", "Invalid admin token", 401));
|
||||
}
|
||||
return next();
|
||||
}
|
||||
export function requireDeviceToken(req, _res, next) {
|
||||
const raw = req.header("authorization") || "";
|
||||
const token = raw.startsWith("Bearer ") ? raw.slice(7) : raw;
|
||||
if (!token) {
|
||||
return next(new ApiError("UNAUTHORIZED", "Missing device bearer token", 401));
|
||||
}
|
||||
if (token !== env.DEVICE_TOKEN) {
|
||||
return next(new ApiError("UNAUTHORIZED", "Invalid device token", 401));
|
||||
}
|
||||
return next();
|
||||
}
|
||||
Reference in New Issue
Block a user