payload-gatekeeper
Version:
The ultimate access control gatekeeper for Payload CMS v3 - Advanced RBAC with wildcard support, auto role assignment, and flexible configuration
35 lines • 1.49 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAfterReadHook = void 0;
const getRolesSlug_1 = require("../utils/getRolesSlug");
/**
* Creates an afterRead hook that populates the user's role when the user is loaded
* This ensures the role object is always available when the user is fetched
* This runs after authentication and whenever the user document is read
*/
const createAfterReadHook = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return async ({ doc, req }) => {
// If doc has a role, and it's just an ID (string or number), populate it
if (doc?.role && (typeof doc.role === 'string' || typeof doc.role === 'number')) {
try {
const role = await req.payload.findByID({
collection: (0, getRolesSlug_1.getRolesSlug)(),
id: String(doc.role),
depth: 0, // Don't need nested data
});
if (role) {
// Replace the role ID with the full role object
doc.role = role;
}
}
catch (error) {
// Log but don't fail the read operation if role population fails
console.warn('Could not populate role in afterRead:', error);
}
}
return doc;
};
};
exports.createAfterReadHook = createAfterReadHook;
//# sourceMappingURL=afterReadHook.js.map
;