File size: 1,058 Bytes
da819ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const auth = (req, res, next) => {
  try {
    const token = req.header('Authorization')?.replace('Bearer ', '');
    const userRole = req.header('user-role');
    
    if (!token) {
      return res.status(401).json({
        success: false,
        message: 'No token provided'
      });
    }

    // Check if token is in the simplified format (user_ or visitor_)
    if (token.startsWith('user_') || token.startsWith('visitor_')) {
      // For simplified system, include user role from header
      const userInfo = req.header('user-info');
      req.user = { 
        token,
        role: userRole || 'visitor', // Default to visitor if no role provided
        userInfo: userInfo ? JSON.parse(userInfo) : {}
      };
      next();
    } else {
      return res.status(401).json({
        success: false,
        message: 'Invalid token format'
      });
    }
  } catch (error) {
    console.error('Auth middleware error:', error);
    res.status(401).json({
      success: false,
      message: 'Invalid token'
    });
  }
};

module.exports = auth;