This commit is contained in:
magdo
2025-10-15 17:01:52 +02:00
parent a25807aca1
commit bec9d83ef3
6 changed files with 14 additions and 43 deletions
@@ -6,7 +6,7 @@ import { logAuth, logWarning } from './Logger';
export const jwtService = new JWTService();
const redisService = RedisService.getInstance();
/**
/**
* Check if a token is blacklisted
*/
async function isTokenBlacklisted(token: string): Promise<boolean> {
@@ -23,9 +23,9 @@ async function isTokenBlacklisted(token: string): Promise<boolean> {
/**
* Extract token from request (cookie or Authorization header)
*/
function extractToken(req: Request): string | null {
function extractToken(req: Request, type: 'auth' | 'refresh'): string | null {
// First try to get token from cookie
const cookieToken = req.cookies['auth_token'];
const cookieToken = req.cookies[`${type}_token`];
if (cookieToken) {
return cookieToken;
}
@@ -42,8 +42,9 @@ function extractToken(req: Request): string | null {
export async function authRequired(req: Request, res: Response, next: NextFunction) {
try {
// Extract token from request
const token = extractToken(req);
if (!token) {
const token = extractToken(req, "auth");
const refreshToken = extractToken(req, "refresh");
if (!token || !refreshToken) {
logAuth('Authentication failed - No token provided', undefined, {
ip: req.ip,
userAgent: req.get ? req.get('User-Agent') : 'unknown',
@@ -95,8 +96,9 @@ export async function authRequired(req: Request, res: Response, next: NextFuncti
export async function adminRequired(req: Request, res: Response, next: NextFunction) {
try {
// Extract token from request
const token = extractToken(req);
if (!token) {
const token = extractToken(req, "auth");
const refreshToken = extractToken(req, "refresh");
if (!token || !refreshToken) {
logWarning('Admin access denied - No token provided', {
ip: req.ip,
path: req.path