代理后端出版
This commit is contained in:
@@ -10,9 +10,10 @@ const JWT_SECRET = process.env.JWT_SECRET || 'agent_jwt_secret_key_2024';
|
||||
* 验证JWT token并确保用户是激活的代理
|
||||
*/
|
||||
const agentAuth = async (req, res, next) => {
|
||||
try {
|
||||
const token = req.headers.authorization?.replace('Bearer ', '');
|
||||
try {
|
||||
const authHeader = req.header('Authorization');
|
||||
|
||||
const token = authHeader?.replace('Bearer ', '');
|
||||
if (!token) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
@@ -23,6 +24,7 @@ const agentAuth = async (req, res, next) => {
|
||||
// 验证JWT token
|
||||
const decoded = jwt.verify(token, JWT_SECRET);
|
||||
|
||||
|
||||
// 检查是否是代理角色
|
||||
if (decoded.role !== 'agent') {
|
||||
return res.status(403).json({
|
||||
@@ -45,7 +47,6 @@ const agentAuth = async (req, res, next) => {
|
||||
LEFT JOIN users u ON ra.user_id = u.id
|
||||
WHERE ra.id = ?
|
||||
`, [decoded.agentId]);
|
||||
|
||||
if (agents.length === 0) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
@@ -81,10 +82,12 @@ const agentAuth = async (req, res, next) => {
|
||||
next();
|
||||
|
||||
} catch (error) {
|
||||
console.log(error,'regional_agents');
|
||||
|
||||
if (error.name === 'JsonWebTokenError') {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: '无效的认证令牌'
|
||||
message: '无效的认证令牌1'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user