Skip to content

feat: add possibility to pass function into the loginPromptHTML #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions adminforth/commands/createApp/templates/index.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export const admin = new AdminForth({
rememberMeDays: 30,
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',
loginBackgroundPosition: '1/2',
loginPromptHTML: async () => {
const adminforthUserExists = await admin.resource("users").count(Filters.EQ('email', 'adminforth')) > 0;
if (adminforthUserExists) {
return "Please use <b>adminforth</b> as username and <b>adminforth</b> as password"
}
},
},
customization: {
brandName: "{{appName}}",
Expand Down
12 changes: 8 additions & 4 deletions adminforth/modules/restApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
IAdminForthAndOrFilter,
} from "../types/Back.js";

import { ADMINFORTH_VERSION, listify, md5hash } from './utils.js';
import { ADMINFORTH_VERSION, listify, md5hash, getLoginPromptHTML } from './utils.js';

import AdminForthAuth from "../auth.js";
import { ActionCheckSource, AdminForthConfigMenuItem, AdminForthDataTypes, AdminForthFilterOperators, AdminForthResourceCommon, AdminForthResourcePages,
Expand Down Expand Up @@ -210,14 +210,16 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
const resource = this.adminforth.config.resources.find((res) => res.resourceId === this.adminforth.config.auth.usersResourceId);
const usernameColumn = resource.columns.find((col) => col.name === usernameField);

const loginPromptHTML = await getLoginPromptHTML(this.adminforth.config.auth.loginPromptHTML);

return {
brandName: this.adminforth.config.customization.brandName,
usernameFieldName: usernameColumn.label,
loginBackgroundImage: this.adminforth.config.auth.loginBackgroundImage,
loginBackgroundPosition: this.adminforth.config.auth.loginBackgroundPosition,
title: this.adminforth.config.customization?.title,
demoCredentials: this.adminforth.config.auth.demoCredentials,
loginPromptHTML: await tr(this.adminforth.config.auth.loginPromptHTML, 'system.loginPromptHTML'),
loginPromptHTML: await tr(loginPromptHTML, 'system.loginPromptHTML'),
loginPageInjections: this.adminforth.config.customization.loginPageInjections,
globalInjections: {
everyPageBottom: this.adminforth.config.customization.globalInjections.everyPageBottom,
Expand All @@ -227,7 +229,6 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
},
});


server.endpoint({
method: 'GET',
path: '/get_base_config',
Expand Down Expand Up @@ -290,6 +291,9 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
}

const announcementBadge: AnnouncementBadgeResponse = this.adminforth.config.customization.announcementBadge?.(adminUser);

const loginPromptHTML = await getLoginPromptHTML(this.adminforth.config.auth.loginPromptHTML);


const publicPart = {
brandName: this.adminforth.config.customization.brandName,
Expand All @@ -298,7 +302,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
loginBackgroundPosition: this.adminforth.config.auth.loginBackgroundPosition,
title: this.adminforth.config.customization?.title,
demoCredentials: this.adminforth.config.auth.demoCredentials,
loginPromptHTML: await tr(this.adminforth.config.auth.loginPromptHTML, 'system.loginPromptHTML'),
loginPromptHTML: await tr(loginPromptHTML, 'system.loginPromptHTML'),
loginPageInjections: this.adminforth.config.customization.loginPageInjections,
rememberMeDays: this.adminforth.config.auth.rememberMeDays,
}
Expand Down
10 changes: 10 additions & 0 deletions adminforth/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fileURLToPath } from 'url';
import fs from 'fs';
import Fuse from 'fuse.js';
import crypto from 'crypto';
import AdminForth, { AdminForthConfig } from '../index.js';
// @ts-ignore-next-line


Expand Down Expand Up @@ -158,6 +159,15 @@ const csscolors = {
}


export async function getLoginPromptHTML(loginPrompt: string | Function) {
if(typeof loginPrompt === 'function') {
loginPrompt = await loginPrompt();
if (!loginPrompt) {
return null;
}
}
return loginPrompt;
}

export function guessLabelFromName(name) {
if (name.includes('_')) {
Expand Down
2 changes: 1 addition & 1 deletion adminforth/types/Back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ export interface AdminForthInputConfig {
/**
* Any prompt to show users on login. Supports HTML.
*/
loginPromptHTML?: string,
loginPromptHTML?: string | (() => string | void | undefined | Promise<string | void | undefined>) | undefined

/**
* Remember me days for "Remember Me" checkbox on login page.
Expand Down
2 changes: 1 addition & 1 deletion adminforth/types/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ export interface AdminForthConfigForFrontend {
loginBackgroundPosition: string,
title?: string,
demoCredentials?: string,
loginPromptHTML?: string,
loginPromptHTML?: string | (() => string | Promise<string> | void | Promise<void> | Promise<undefined>) | undefined
loginPageInjections: {
underInputs: Array<AdminForthComponentDeclaration>,
panelHeader: Array<AdminForthComponentDeclaration>,
Expand Down
9 changes: 7 additions & 2 deletions dev-demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ export const admin = new AdminForth({
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',
loginBackgroundPosition: '1/2', // over, 3/4, 2/5, 3/5 (tailwind grid)
demoCredentials: "adminforth:adminforth", // never use it for production
loginPromptHTML: "Use email <b>adminforth</b> and password <b>adminforth</b> to login",
loginPromptHTML: async () => {
const adminforthUserExists = await admin.resource("users").count(Filters.EQ('email', 'adminforth')) > 0;
if (adminforthUserExists) {
return "Please use <b>adminforth</b> as username and <b>adminforth</b> as password"
}
},
// loginBackgroundImage: '@@/pho.jpg',
rememberMeDays: 30,
beforeLoginConfirmation: [async ({adminUser, adminforth, extra}) => {
Expand Down Expand Up @@ -202,7 +207,7 @@ export const admin = new AdminForth({
},
{
id: 'ch',
url: 'clickhouse://demo:demo@localhost:8125/demo',
url: 'clickhouse://demo:demo@localhost:8124/demo',
},
{
id: 'mysql',
Expand Down