Files
klz-cables.com/.pnpm-store/v10/files/2c/3f69b34cf451029014ff775437649c5b494f96fb8939178549f2951d3680c8779f2056326425684af3b9187eea3326fe2e564bb9ca53fc65b79d4e54c897b1
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

39 lines
1.6 KiB
Plaintext

import { UnauthorizedError } from '../errors/UnauthorizedError.js';
/**
* Protects admin-only routes, server functions, etc.
* The requesting user must either:
* a. pass the `access.admin` function on the `users` collection, if defined
* b. match the `config.admin.user` property on the Payload config
* c. if no user is present, and there are no users in the system, allow access (for first user creation)
* @throws {Error} Throws an `Unauthorized` error if access is denied that can be explicitly caught
*/ export const canAccessAdmin = async ({ req })=>{
const incomingUserSlug = req.user?.collection;
const adminUserSlug = req.payload.config.admin.user;
if (incomingUserSlug) {
const adminAccessFn = req.payload.collections[incomingUserSlug]?.config.access?.admin;
if (adminAccessFn) {
const canAccess = await adminAccessFn({
req
});
if (!canAccess) {
throw new UnauthorizedError();
}
// Match the user collection to the global admin config
} else if (adminUserSlug !== incomingUserSlug) {
throw new UnauthorizedError();
}
} else {
const hasUsers = await req.payload.find({
collection: adminUserSlug,
depth: 0,
limit: 1,
pagination: false
});
// If there are users, we should not allow access because of `/create-first-user`
if (hasUsers.docs.length) {
throw new UnauthorizedError();
}
}
};
//# sourceMappingURL=canAccessAdmin.js.map