29 lines
761 B
JavaScript
29 lines
761 B
JavaScript
|
import Log from "../models/Log.js";
|
||
|
|
||
|
const logApiCall = async (req, res, next) => {
|
||
|
try {
|
||
|
// Assuming verifyToken middleware attaches the user info to req.user
|
||
|
const userId = req.user.id;
|
||
|
|
||
|
// Create a log entry
|
||
|
const logEntry = new Log({
|
||
|
userId: userId,
|
||
|
method: req.method,
|
||
|
endpoint: req.originalUrl,
|
||
|
body: req.body,
|
||
|
});
|
||
|
|
||
|
// Save the log entry to the database
|
||
|
await logEntry.save();
|
||
|
|
||
|
// Proceed to the next middleware or route handler
|
||
|
next();
|
||
|
} catch (error) {
|
||
|
console.error("Error logging API call:", error);
|
||
|
// Optionally, you can handle the error here (e.g., send an error response)
|
||
|
next(error); // Pass the error to the error-handling middleware
|
||
|
}
|
||
|
};
|
||
|
|
||
|
export default logApiCall;
|