SurfSense/surfsense_web/lib/runtime-auth-config.ts
Anish Sarkar b54eff648e feat: implement runtime authentication handling
- Added a new proxy function to manage runtime authentication types and set cookies accordingly.
- Introduced runtime authentication configuration to dynamically adjust UI based on the selected auth type.
- Updated global styles to hide specific authentication buttons based on the current auth type.
- Refactored sign-in button and hero section components to utilize the new runtime authentication logic.
- Created a new utility file for runtime authentication configuration and initialization script.
2026-06-19 03:56:26 +05:30

52 lines
1.7 KiB
TypeScript

export const RUNTIME_AUTH_TYPE_COOKIE_NAME = "surfsense_auth_type";
export type RuntimeAuthUiMode = "GOOGLE" | "LOCAL";
export function resolveRuntimeAuthUiMode(
value: string | null | undefined,
fallback: string | null | undefined = "GOOGLE"
): RuntimeAuthUiMode {
const candidate = value?.trim().toUpperCase();
if (candidate === "GOOGLE") return "GOOGLE";
if (candidate === "LOCAL") return "LOCAL";
const fallbackCandidate = fallback?.trim().toUpperCase();
return fallbackCandidate === "GOOGLE" ? "GOOGLE" : "LOCAL";
}
export function getRuntimeAuthInitScript(fallbackAuthType: string): string {
const fallback = resolveRuntimeAuthUiMode(fallbackAuthType);
const cookieName = JSON.stringify(RUNTIME_AUTH_TYPE_COOKIE_NAME);
const fallbackValue = JSON.stringify(fallback);
return `
(function() {
try {
var cookieName = ${cookieName};
var fallback = ${fallbackValue};
var prefix = cookieName + "=";
var rawValue = fallback;
var cookies = document.cookie ? document.cookie.split(";") : [];
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.indexOf(prefix) === 0) {
rawValue = decodeURIComponent(cookie.slice(prefix.length));
break;
}
}
var normalized = String(rawValue || fallback).toUpperCase() === "GOOGLE" ? "GOOGLE" : "LOCAL";
window.__SURFSENSE_AUTH_TYPE__ = normalized;
document.documentElement.setAttribute("data-surfsense-auth-type", normalized);
} catch (_) {
window.__SURFSENSE_AUTH_TYPE__ = ${fallbackValue};
document.documentElement.setAttribute("data-surfsense-auth-type", ${fallbackValue});
}
})();
`;
}
declare global {
interface Window {
__SURFSENSE_AUTH_TYPE__?: RuntimeAuthUiMode;
}
}