Files
homebrewery-oidc/client/fork/auth/loginUrl.js
T
dev-bot BotandClaude Sonnet 5.5 fe278d465f feat(auth): generischer OIDC-Login (Code+PKCE), Provider abschaltbar
Modul server/fork/auth (Config, Session-Cookie, OIDC-Flow, Kontozuordnung), Einhängen in server/app.js,
Client-Helfer für Login/Logout, PUT /api/user/rename für OIDC-Sessions gesperrt (A01). Tests gegen Mock-IdP.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 09:10:28 +00:00

34 lines
1.5 KiB
JavaScript

// Login-/Logout-Helfer für den Client (ADR 0001 §6). Konfiguration kommt aus configuration.auth (Server).
const NATURALCRIT_LOGIN = 'https://www.naturalcrit.com/login';
const authConfig = ()=>(typeof global !== 'undefined' && global.config?.auth) || null;
export const oidcEnabled = ()=>!!authConfig()?.providers?.includes('oidc');
export const naturalcritEnabled = ()=>{
const providers = authConfig()?.providers;
return !providers || providers.includes('naturalcrit');
};
export const getOidcLabel = ()=>authConfig()?.oidcLabel || 'Anmelden';
// redirect: Pfad (mit Query) oder absolute URL; bei OIDC wird nur der Pfad verwendet
export const getLoginUrl = (redirect = '/')=>{
const cfg = authConfig();
if(cfg?.loginUrl) {
let path = redirect;
try {
const u = new URL(redirect, typeof window !== 'undefined' ? window.location.href : 'http://localhost');
path = `${u.pathname}${u.search}${u.hash}`;
} catch { path = '/'; }
return `${cfg.loginUrl}?redirect=${encodeURIComponent(path)}`;
}
if(!redirect || redirect === '/') return NATURALCRIT_LOGIN;
const base = redirect.startsWith('/') ? (global.config?.baseUrl || global.config?.publicUrl || '') : '';
return `${NATURALCRIT_LOGIN}?redirect=${base}${redirect}`;
};
export const getLogoutUrl = ()=>authConfig()?.logoutUrl || '/auth/logout';
// true, wenn der Client den Logout dem Server überlassen muss (httpOnly-Cookie)
export const logoutViaServer = (account = typeof global !== 'undefined' ? global.account : undefined)=>account?.provider === 'oidc';