Auth implemented
This commit is contained in:
21
backend/src/middleware/authMiddleware.ts
Normal file
21
backend/src/middleware/authMiddleware.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
// backend/src/middleware/authMiddleware.ts
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { SessionService } from '../services/SessionService';
|
||||
|
||||
export const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
|
||||
const sessionToken = req.headers['x-session-token'] as string; // Assuming token is sent in a header
|
||||
|
||||
if (!sessionToken) {
|
||||
return res.status(401).json({ message: 'No session token provided.' });
|
||||
}
|
||||
|
||||
const session = SessionService.getSession(sessionToken);
|
||||
|
||||
if (!session || !session.isAuthenticated) {
|
||||
return res.status(401).json({ message: 'Invalid or unauthenticated session.' });
|
||||
}
|
||||
|
||||
// Optionally, attach session to request for further use
|
||||
(req as any).session = session;
|
||||
next();
|
||||
};
|
||||
Reference in New Issue
Block a user