I'm making an landing page for an nextJS 14 app and using payload as a backend. I'm creating blogs in the payload and access them in the frontend app.
Now is there any way to secure these endpoints currently I'm using cors option in the buildConfig.ts.
And one option I'm using is custom hook in the collection using beforeRead
`hooks: {
afterChange: [revalidatePost],
afterRead: [populateAuthors],
afterDelete: [revalidateDelete],
beforeRead: [
async ({ req, query }) => {
const apiKey = req.headers.get('x-api-key')
// Check if the API key is provided and matches the one stored in the environment variables
if (apiKey !== process.env.PAYLOAD_API_KEY) {
throw new Error('Forbidden: Invalid API Key')
}
return query
},
],
},This approach works but now I've to write thisbeforeRead` to every collection, Is there any way to do this globally.