Default “where” criteria for user with blueprint find
and update
actions.
Note: Before using
req.options.where
, confirm that it exists and create it if necessary.
To default to finding only records where userId
matches the logged-in user’s id:
// In config/policies/filterByUser.js
module.exports = function filterByUser (req, res, next) {
if (req.session.user) {
// Use existing req.options.where, or initialize it to an empty object
req.options.where = req.options.where || {};
// Set the default `userId` for "find" and "update" blueprints
req.options.where.userId = req.session.user.id;
}
return next();
}
Then apply this policy to the desired blueprint actions.