Skip to content

Commit 2ebd9b6

Browse files
authored
Merge pull request #224 from devforth/beforeLoginConfirmation-fix
fix: Support both single function and array for beforeLoginConfirmation
2 parents fb5855a + 5b4aeef commit 2ebd9b6

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

adminforth/modules/configValidator.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,16 @@ export default class ConfigValidator implements IConfigValidator {
942942
throw new Error(`Resource with id "${newConfig.auth.usersResourceId}" not found. ${similar ? `Did you mean "${similar}"?` : ''}`);
943943
}
944944

945-
if (!newConfig.auth.beforeLoginConfirmation) {
946-
newConfig.auth.beforeLoginConfirmation = [];
945+
// normalize beforeLoginConfirmation hooks
946+
const blc = this.inputConfig.auth.beforeLoginConfirmation;
947+
if (!Array.isArray(blc)) {
948+
if (blc) {
949+
newConfig.auth.beforeLoginConfirmation = [blc];
950+
} else {
951+
newConfig.auth.beforeLoginConfirmation = [];
952+
}
953+
} else {
954+
newConfig.auth.beforeLoginConfirmation = blc;
947955
}
948956
}
949957

adminforth/modules/restApi.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,21 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
8484

8585
async processLoginCallbacks(adminUser: AdminUser, toReturn: { redirectTo?: string, allowedLogin:boolean, error?: string }, response: any, extra: HttpExtra) {
8686
const beforeLoginConfirmation = this.adminforth.config.auth.beforeLoginConfirmation as (BeforeLoginConfirmationFunction[] | undefined);
87-
if (beforeLoginConfirmation?.length){
88-
for (const hook of beforeLoginConfirmation) {
89-
const resp = await hook({
90-
adminUser,
91-
response,
92-
adminforth: this.adminforth,
93-
extra,
94-
});
95-
96-
if (resp?.body?.redirectTo || resp?.error) {
97-
// delete all items from toReturn and add these:
98-
toReturn.redirectTo = resp?.body?.redirectTo;
99-
toReturn.allowedLogin = resp?.body?.allowedLogin;
100-
toReturn.error = resp?.error;
101-
break;
102-
}
87+
88+
for (const hook of listify(beforeLoginConfirmation)) {
89+
const resp = await hook({
90+
adminUser,
91+
response,
92+
adminforth: this.adminforth,
93+
extra,
94+
});
95+
96+
if (resp?.body?.redirectTo || resp?.error) {
97+
// delete all items from toReturn and add these:
98+
toReturn.redirectTo = resp?.body?.redirectTo;
99+
toReturn.allowedLogin = resp?.body?.allowedLogin;
100+
toReturn.error = resp?.error;
101+
break;
103102
}
104103
}
105104
}

0 commit comments

Comments
 (0)