Improve user session management and authentication for enhanced security
Integrates Passport.js sessions and updates authentication middleware logic in `auth.ts` and `routes.ts`. Replit-Commit-Author: Agent Replit-Commit-Session-Id: c5f0c281-8dd8-4846-b452-4a07bcd21062 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/9777c70b-fc38-4831-8d6b-78dfffe041b0/d1d8abd3-0b74-4353-84ac-e56e863e16e5.jpg
This commit is contained in:
@@ -6,18 +6,16 @@ export interface AuthenticatedRequest extends Request {
|
||||
}
|
||||
|
||||
export const authMiddleware = async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
||||
if (!req.user) {
|
||||
if (!req.isAuthenticated || !req.isAuthenticated()) {
|
||||
return res.status(401).json({ error: 'Authentication required' });
|
||||
}
|
||||
|
||||
try {
|
||||
// Refresh user data from database
|
||||
const user = await storage.getUser(req.user.id);
|
||||
if (!user) {
|
||||
// User is already available from Passport session
|
||||
if (!req.user) {
|
||||
return res.status(401).json({ error: 'User not found' });
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error('Auth middleware error:', error);
|
||||
|
||||
Reference in New Issue
Block a user