Skip to content

Commit

Permalink
Support async preRegisterCheck for custom modules (directus#6118)
Browse files Browse the repository at this point in the history
* Support async preRegisterCheck for custom modules

* Make loop a tad more expressive

* Update type to indicate potential promise return

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
  • Loading branch information
t7tran and rijkvanzanten authored Jun 9, 2021
1 parent a2a35aa commit 7ecbe49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions app/src/modules/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ export async function register(): Promise<void> {
const userStore = useUserStore();
const permissionsStore = usePermissionsStore();

const registeredModules = queuedModules.filter((mod: any) => {
if (!userStore.currentUser) return false;
const registeredModules = [];

for (const mod of queuedModules) {
if (!userStore.currentUser) continue;

if (mod.preRegisterCheck) {
return mod.preRegisterCheck(userStore.currentUser, permissionsStore.permissions);
const allowed = await mod.preRegisterCheck(userStore.currentUser, permissionsStore.permissions);
if (allowed) registeredModules.push(mod);
} else {
registeredModules.push(mod);
}

return true;
});
}

for (const module of registeredModules) {
router.addRoute({
Expand Down
2 changes: 1 addition & 1 deletion app/src/modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ModuleConfig {
routes?: RouteRecordRaw[];
link?: string;
color?: string;
preRegisterCheck?: (user: User, permissions: Permission[]) => boolean;
preRegisterCheck?: (user: User, permissions: Permission[]) => Promise<boolean> | boolean;
order?: number;
persistent?: boolean;
}
Expand Down

0 comments on commit 7ecbe49

Please sign in to comment.