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 | 3x 3x 57x 7x 3x 3x 2x 2x 1x 7x | import { getRolesSlug } from '../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
*/
export const createAfterReadHook = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return async ({ doc, req }: any) => {
// 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: getRolesSlug() as 'roles',
id: String(doc.role),
depth: 0, // Don't need nested data
})
Eif (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
}
}
|