您可以在isAuthenticated方法中添加API密钥并检查此密钥。如果密钥可用,请调用next()函数。
代码段:
module.exports = function (req, res, next) { if (req.isAuthenticated() || req.headers['api-key'] == 'apiKeyValue') { return next(); } else { if (!req.headers['api-key'] && !req.isAuthenticated()) { return res.redirect('/auth/login'); } else { res.status(403); return res.json({ error: 'Invalid Api Key' }); } }
基本上,通过标头传递API密钥并修改isAuthenticated函数中的逻辑。因此,只要API密钥可用,它就会调用next()回调。