27 lines
954 B
JavaScript
27 lines
954 B
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.protect = void 0;
|
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
const JWT_SECRET = process.env.JWT_SECRET || 'your_super_secret_jwt_key';
|
|
const protect = (req, res, next) => {
|
|
let token;
|
|
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer')) {
|
|
token = req.headers.authorization.split(' ')[1];
|
|
}
|
|
if (!token) {
|
|
return res.status(401).json({ error: 'Not authorized, no token' });
|
|
}
|
|
try {
|
|
const decoded = jsonwebtoken_1.default.verify(token, JWT_SECRET);
|
|
req.user = decoded;
|
|
next();
|
|
}
|
|
catch (error) {
|
|
return res.status(401).json({ error: 'Not authorized, token failed' });
|
|
}
|
|
};
|
|
exports.protect = protect;
|