43 lines
1.7 KiB
TypeScript
43 lines
1.7 KiB
TypeScript
import type { AuthenticationOptions } from "./account-api";
|
|
|
|
/** A missing options response must never fall back to a legacy guest login. */
|
|
export function shouldStartSso(
|
|
options: AuthenticationOptions | null,
|
|
explicitSso: boolean,
|
|
registering: boolean,
|
|
sessionExpired = false,
|
|
): boolean {
|
|
return Boolean(options?.sso_enabled && !registering && !sessionExpired &&
|
|
(explicitSso || !options.password_login_enabled));
|
|
}
|
|
|
|
/** External callers put login parameters before or after the hash route. */
|
|
export function mergeLoginSearch(routeSearch: string, outerSearch: string): URLSearchParams {
|
|
const params = new URLSearchParams(outerSearch);
|
|
// A route parameter wins even when explicitly empty: do not revive an old
|
|
// outer credential when the current route has replaced it.
|
|
new URLSearchParams(routeSearch).forEach((value, key) => params.set(key, value));
|
|
return params;
|
|
}
|
|
|
|
export const LOGIN_CREDENTIAL_PARAMS = [
|
|
"ticket", "userId", "yUserId", "token", "access_token", "password", "authToken", "routeKey",
|
|
] as const;
|
|
|
|
/** Cached tickets belong only to the legacy, bare entry with login disabled. */
|
|
export function shouldUseStoredTicket(
|
|
options: AuthenticationOptions | null,
|
|
explicitSso: boolean,
|
|
): boolean {
|
|
return Boolean(options?.sso_enabled && !options.password_login_enabled && !explicitSso);
|
|
}
|
|
|
|
/** Keep the destination/presentation when switching forms; discard credentials. */
|
|
export function accountPageSearch(search: string, routeKeyTarget?: string): string {
|
|
const params = new URLSearchParams(search);
|
|
if (routeKeyTarget) params.set("redirect", routeKeyTarget);
|
|
for (const key of LOGIN_CREDENTIAL_PARAMS) params.delete(key);
|
|
const result = params.toString();
|
|
return result ? `?${result}` : "";
|
|
}
|