Skip to content

Commit 141cf85

Browse files
authored
Merge pull request #273 from devforth/AdminForth/711
feat: add possibility to pass function into the loginPromptHTML
2 parents 7658d56 + a76338d commit 141cf85

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

adminforth/commands/createApp/templates/index.ts.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export const admin = new AdminForth({
1515
rememberMeDays: 30,
1616
loginBackgroundImage: 'https://images.unsplash.com/photo-1534239697798-120952b76f2b?q=80&w=3389&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
1717
loginBackgroundPosition: '1/2',
18+
loginPromptHTML: async () => {
19+
const adminforthUserExists = await admin.resource("users").count(Filters.EQ('email', 'adminforth')) > 0;
20+
if (adminforthUserExists) {
21+
return "Please use <b>adminforth</b> as username and <b>adminforth</b> as password"
22+
}
23+
},
1824
},
1925
customization: {
2026
brandName: "{{appName}}",

adminforth/modules/restApi.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
IAdminForthAndOrFilter,
1414
} from "../types/Back.js";
1515

16-
import { ADMINFORTH_VERSION, listify, md5hash } from './utils.js';
16+
import { ADMINFORTH_VERSION, listify, md5hash, getLoginPromptHTML } from './utils.js';
1717

1818
import AdminForthAuth from "../auth.js";
1919
import { ActionCheckSource, AdminForthConfigMenuItem, AdminForthDataTypes, AdminForthFilterOperators, AdminForthResourceCommon, AdminForthResourcePages,
@@ -210,6 +210,8 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
210210
const resource = this.adminforth.config.resources.find((res) => res.resourceId === this.adminforth.config.auth.usersResourceId);
211211
const usernameColumn = resource.columns.find((col) => col.name === usernameField);
212212

213+
const loginPromptHTML = await getLoginPromptHTML(this.adminforth.config.auth.loginPromptHTML);
214+
213215
return {
214216
brandName: this.adminforth.config.customization.brandName,
215217
usernameFieldName: usernameColumn.label,
@@ -218,7 +220,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
218220
removeBackgroundBlendMode: this.adminforth.config.auth.removeBackgroundBlendMode,
219221
title: this.adminforth.config.customization?.title,
220222
demoCredentials: this.adminforth.config.auth.demoCredentials,
221-
loginPromptHTML: await tr(this.adminforth.config.auth.loginPromptHTML, 'system.loginPromptHTML'),
223+
loginPromptHTML: await tr(loginPromptHTML, 'system.loginPromptHTML'),
222224
loginPageInjections: this.adminforth.config.customization.loginPageInjections,
223225
globalInjections: {
224226
everyPageBottom: this.adminforth.config.customization.globalInjections.everyPageBottom,
@@ -230,7 +232,6 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
230232
},
231233
});
232234

233-
234235
server.endpoint({
235236
method: 'GET',
236237
path: '/get_base_config',
@@ -293,6 +294,9 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
293294
}
294295

295296
const announcementBadge: AnnouncementBadgeResponse = this.adminforth.config.customization.announcementBadge?.(adminUser);
297+
298+
const loginPromptHTML = await getLoginPromptHTML(this.adminforth.config.auth.loginPromptHTML);
299+
296300

297301
const publicPart = {
298302
brandName: this.adminforth.config.customization.brandName,
@@ -302,7 +306,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
302306
removeBackgroundBlendMode: this.adminforth.config.auth.removeBackgroundBlendMode,
303307
title: this.adminforth.config.customization?.title,
304308
demoCredentials: this.adminforth.config.auth.demoCredentials,
305-
loginPromptHTML: await tr(this.adminforth.config.auth.loginPromptHTML, 'system.loginPromptHTML'),
309+
loginPromptHTML: await tr(loginPromptHTML, 'system.loginPromptHTML'),
306310
loginPageInjections: this.adminforth.config.customization.loginPageInjections,
307311
rememberMeDays: this.adminforth.config.auth.rememberMeDays,
308312
singleTheme: this.adminforth.config.customization.singleTheme,

adminforth/modules/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fileURLToPath } from 'url';
33
import fs from 'fs';
44
import Fuse from 'fuse.js';
55
import crypto from 'crypto';
6+
import AdminForth, { AdminForthConfig } from '../index.js';
67
// @ts-ignore-next-line
78

89

@@ -158,6 +159,15 @@ const csscolors = {
158159
}
159160

160161

162+
export async function getLoginPromptHTML(loginPrompt: string | Function) {
163+
if(typeof loginPrompt === 'function') {
164+
loginPrompt = await loginPrompt();
165+
if (!loginPrompt) {
166+
return null;
167+
}
168+
}
169+
return loginPrompt;
170+
}
161171

162172
export function guessLabelFromName(name) {
163173
if (name.includes('_')) {

adminforth/types/Back.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ export interface AdminForthInputConfig {
978978
/**
979979
* Any prompt to show users on login. Supports HTML.
980980
*/
981-
loginPromptHTML?: string,
981+
loginPromptHTML?: string | (() => string | void | undefined | Promise<string | void | undefined>) | undefined
982982

983983
/**
984984
* Remember me days for "Remember Me" checkbox on login page.

adminforth/types/Common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ export interface AdminForthConfigForFrontend {
10601060
removeBackgroundBlendMode: boolean,
10611061
title?: string,
10621062
demoCredentials?: string,
1063-
loginPromptHTML?: string,
1063+
loginPromptHTML?: string | (() => string | Promise<string> | void | Promise<void> | Promise<undefined>) | undefined
10641064
loginPageInjections: {
10651065
underInputs: Array<AdminForthComponentDeclaration>,
10661066
panelHeader: Array<AdminForthComponentDeclaration>,

dev-demo/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ export const admin = new AdminForth({
7878
loginBackgroundImage: 'https://images.unsplash.com/photo-1534239697798-120952b76f2b?q=80&w=3389&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
7979
loginBackgroundPosition: '1/2', // over, 3/4, 2/5, 3/5 (tailwind grid)
8080
demoCredentials: "adminforth:adminforth", // never use it for production
81-
loginPromptHTML: "Use email <b>adminforth</b> and password <b>adminforth</b> to login",
81+
loginPromptHTML: async () => {
82+
const adminforthUserExists = await admin.resource("users").count(Filters.EQ('email', 'adminforth')) > 0;
83+
if (adminforthUserExists) {
84+
return "Please use <b>adminforth</b> as username and <b>adminforth</b> as password"
85+
}
86+
},
8287
// loginBackgroundImage: '@@/pho.jpg',
8388
rememberMeDays: 30,
8489
beforeLoginConfirmation: [async ({adminUser, adminforth, extra}) => {
@@ -202,7 +207,7 @@ export const admin = new AdminForth({
202207
},
203208
{
204209
id: 'ch',
205-
url: 'clickhouse://demo:demo@localhost:8125/demo',
210+
url: 'clickhouse://demo:demo@localhost:8124/demo',
206211
},
207212
{
208213
id: 'mysql',

0 commit comments

Comments
 (0)