File size: 3,665 Bytes
28e1dba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import express from 'express';
import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import { USERS, JWT_SECRET, JWT_EXPIRES_IN, JWT_ENABLED } from '../config/users.js';

const router = express.Router();

// 登录
router.post('/login', async (req, res, next) => {
  try {
    const { username, password } = req.body;

    // 如果JWT未启用,返回默认用户
    if (!JWT_ENABLED) {
      console.log('JWT disabled, returning default user for login');
      return res.json({
        token: 'no-auth-required',
        user: {
          id: 'PS01',
          username: 'PS01',
          role: 'admin'
        },
        message: 'Authentication disabled'
      });
    }

    if (!username || !password) {
      return res.status(400).json({ error: 'Username and password are required' });
    }

    // 查找用户
    const user = USERS.find(u => u.username === username);
    if (!user) {
      return res.status(401).json({ error: 'Invalid credentials' });
    }

    // 验证密码
    if (user.password !== password) {
      return res.status(401).json({ error: 'Invalid credentials' });
    }

    // 生成JWT token
    const token = jwt.sign(
      { 
        userId: user.id, 
        username: user.username, 
        role: user.role 
      },
      JWT_SECRET,
      { expiresIn: JWT_EXPIRES_IN }
    );

    res.json({
      token,
      user: {
        id: user.id,
        username: user.username,
        role: user.role
      }
    });
  } catch (error) {
    next(error);
  }
});

// 验证token
router.get('/verify', (req, res, next) => {
  try {
    // 如果JWT未启用,返回默认用户
    if (!JWT_ENABLED) {
      console.log('JWT disabled, returning default user for verify');
      return res.json({
        user: {
          id: 'PS01',
          username: 'PS01',
          role: 'admin'
        },
        message: 'Authentication disabled'
      });
    }

    const authHeader = req.headers['authorization'];
    const token = authHeader && authHeader.split(' ')[1];

    if (!token) {
      return res.status(401).json({ error: 'No token provided' });
    }

    jwt.verify(token, JWT_SECRET, (err, decoded) => {
      if (err) {
        return res.status(401).json({ error: 'Invalid token' });
      }

      res.json({
        user: {
          id: decoded.userId,
          username: decoded.username,
          role: decoded.role
        }
      });
    });
  } catch (error) {
    next(error);
  }
});

// 获取用户信息
router.get('/user', (req, res, next) => {
  try {
    // 如果JWT未启用,返回默认用户
    if (!JWT_ENABLED) {
      console.log('JWT disabled, returning default user for user info');
      return res.json({
        id: 'PS01',
        username: 'PS01',
        role: 'admin',
        message: 'Authentication disabled'
      });
    }

    const authHeader = req.headers['authorization'];
    const token = authHeader && authHeader.split(' ')[1];

    if (!token) {
      return res.status(401).json({ error: 'No token provided' });
    }

    jwt.verify(token, JWT_SECRET, (err, decoded) => {
      if (err) {
        return res.status(401).json({ error: 'Invalid token' });
      }

      const user = USERS.find(u => u.id === decoded.userId);
      if (!user) {
        return res.status(404).json({ error: 'User not found' });
      }

      res.json({
        id: user.id,
        username: user.username,
        role: user.role
      });
    });
  } catch (error) {
    next(error);
  }
});

export default router;