Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x | const { Flow } = require('../models/Workspace');
/**
* Middleware to authenticate requests using Flow API key
* Header: X-API-Key: mk_...
*/
async function apiKeyAuth(req, res, next) {
try {
const apiKey = req.headers['x-api-key'];
if (!apiKey) {
return res.status(401).json({
success: false,
message: 'API key required. Provide it in X-API-Key header.'
});
}
// Find flow by API key
const flow = await Flow.findOne({
apiKey: apiKey,
isActive: true
}).populate('workspaceId', 'name userId');
if (!flow) {
return res.status(401).json({
success: false,
message: 'Invalid or inactive API key'
});
}
// Attach flow info to request
req.flow = flow;
req.workspace = flow.workspaceId;
req.userId = flow.userId;
next();
} catch (error) {
console.error('API Key Auth Error:', error);
res.status(500).json({
success: false,
message: 'Authentication error',
error: error.message
});
}
}
module.exports = { apiKeyAuth };
|