mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-22 21:28:12 +02:00
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.
This commit is contained in:
parent
03e57bdf7e
commit
b54eff648e
6 changed files with 130 additions and 40 deletions
52
surfsense_web/lib/runtime-auth-config.ts
Normal file
52
surfsense_web/lib/runtime-auth-config.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue