keycloak_theme/src/lib/kcContext.ts

154 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-02-23 15:32:37 +01:00
2021-03-02 12:17:24 +01:00
import { ftlValuesGlobalName } from "../bin/build-keycloak-theme/ftlValuesGlobalName";
2021-03-06 22:41:36 +01:00
import type { PageId } from "../bin/build-keycloak-theme/generateFtl";
import { id } from "evt/tools/typeSafety/id";
2021-03-04 18:15:48 +01:00
import type { KcLanguageTag } from "./i18n/KcLanguageTag";
2021-03-04 21:14:54 +01:00
import { doExtends } from "evt/tools/typeSafety/doExtends";
2021-03-06 14:42:56 +01:00
import type { MessageKey } from "./i18n/useKcTranslation";
import type { LanguageLabel } from "./i18n/KcLanguageTag";
2021-02-23 15:32:37 +01:00
2021-03-06 14:42:56 +01:00
type ExtractAfterStartingWith<Prefix extends string, StrEnum> =
StrEnum extends `${Prefix}${infer U}` ? U : never;
2021-02-23 15:32:37 +01:00
2021-03-07 14:57:53 +01:00
export type KcContext = KcContext.Login | KcContext.Register | KcContext.Info | KcContext.Error | KcContext.LoginResetPassword;
2021-03-04 21:14:54 +01:00
export declare namespace KcContext {
export type Template = {
url: {
loginAction: string;
resourcesPath: string;
resourcesCommonPath: string;
loginRestartFlowUrl: string;
2021-03-02 22:48:36 +01:00
loginUrl: string;
2021-03-04 21:14:54 +01:00
};
realm: {
2021-03-04 18:15:48 +01:00
displayName?: string;
2021-03-04 21:14:54 +01:00
displayNameHtml?: string;
internationalizationEnabled: boolean;
2021-03-06 23:03:03 +01:00
registrationEmailAsUsername: boolean; //<---
2021-03-04 21:14:54 +01:00
};
/** Undefined if !realm.internationalizationEnabled */
locale?: {
supported: {
url: string;
2021-03-04 21:14:54 +01:00
languageTag: KcLanguageTag;
/** Is determined by languageTag. Ex: languageTag === "en" => label === "English"
* or getLanguageLabel(languageTag) === label
*/
//label: LanguageLabel;
}[];
current: LanguageLabel;
2021-03-04 21:14:54 +01:00
},
auth?: {
showUsername: boolean;
showResetCredentials: boolean;
showTryAnotherWayLink: boolean;
2021-03-07 14:57:53 +01:00
attemptedUsername?: string;
2021-03-04 21:14:54 +01:00
};
scripts: string[];
message?: {
type: "success" | "warning" | "error" | "info";
summary: string;
};
isAppInitiatedAction: boolean;
};
export type Login = Template & {
2021-03-06 22:41:36 +01:00
pageId: "login.ftl";
2021-03-04 21:14:54 +01:00
url: {
loginResetCredentialsUrl: string;
registrationUrl: string;
};
realm: {
loginWithEmailAllowed: boolean;
rememberMe: boolean;
2021-03-06 23:03:03 +01:00
password: boolean;
2021-03-04 21:14:54 +01:00
resetPasswordAllowed: boolean;
2021-03-04 23:24:43 +01:00
registrationAllowed: boolean;
2021-03-04 21:14:54 +01:00
};
auth: {
selectedCredential?: string;
};
registrationDisabled: boolean;
login: {
2021-03-04 18:15:48 +01:00
username?: string;
2021-03-04 21:14:54 +01:00
rememberMe: boolean;
};
usernameEditDisabled: boolean;
social: {
displayInfo: boolean;
providers?: {
loginUrl: string;
alias: string;
providerId: string;
displayName: string;
}[]
};
};
export type Register = Template & {
2021-03-06 22:41:36 +01:00
pageId: "register.ftl";
2021-03-04 21:14:54 +01:00
url: {
registrationAction: string;
};
messagesPerField: {
printIfExists<T>(
key:
"userLabel" |
"username" |
"email" |
"firstName" |
"lastName" |
"password" |
"password-confirm",
x: T
): T | undefined;
};
register: {
formData: {
firstName?: string;
displayName?: string;
lastName?: string;
email?: string;
username?: string;
}
};
passwordRequired: boolean;
recaptchaRequired: boolean;
2021-03-05 14:50:46 +01:00
/** undefined if !recaptchaRequired */
recaptchaSiteKey?: string;
2021-03-04 18:15:48 +01:00
};
2021-03-04 21:14:54 +01:00
2021-03-06 14:42:56 +01:00
export type Info = Template & {
2021-03-06 22:41:36 +01:00
pageId: "info.ftl";
2021-03-06 14:42:56 +01:00
messageHeader?: string;
requiredActions?: ExtractAfterStartingWith<"requiredAction.",MessageKey>[];
skipLink: boolean;
pageRedirectUri?: string;
actionUri?: string;
client: {
baseUrl?: string;
}
};
2021-03-06 22:41:36 +01:00
export type Error = Template & {
2021-03-06 23:03:03 +01:00
pageId: "error.ftl";
client?: {
baseUrl?: string;
}
2021-03-06 22:41:36 +01:00
};
2021-03-04 21:14:54 +01:00
2021-03-07 14:57:53 +01:00
export type LoginResetPassword = Template & {
pageId: "login-reset-password.ftl";
realm: {
loginWithEmailAllowed: boolean;
}
};
2021-03-04 21:14:54 +01:00
}
2021-02-23 15:32:37 +01:00
2021-03-06 22:41:36 +01:00
doExtends<KcContext["pageId"], PageId>();
doExtends<PageId, KcContext["pageId"]>();
2021-03-02 23:48:31 +01:00
export const kcContext = id<KcContext | undefined>((window as any)[ftlValuesGlobalName]);