/* This file is part of »dali«. Copyright 2025 'kcf' »dali« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »dali« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »dali«. If not, see . */ namespace _dali { /** */ let _actions_login : Array<((name ?: (null | string)) => Promise)> = []; /** */ let _actions_logout : Array<(() => Promise)> = []; /** */ let _is_logged_in : boolean = false; /** */ export function listen_login( action : ((name ?: (null | string)) => Promise) ) : void { _actions_login.push(action); } /** */ export function listen_logout( action : (() => Promise) ) : void { _actions_logout.push(action); } /** */ export async function notify_login( name : (null | string) ) : Promise { _is_logged_in = true; for (const action of _actions_login) { await action(name); } } /** */ export async function notify_logout( ) : Promise { _is_logged_in = false; for (const action of _actions_logout) { await action(); } } /** */ export function is_logged_in( ) : boolean { return _is_logged_in; } /** */ export async function oidc_finish( session_key : string ) : Promise { await _dali.backend.set_session_key(session_key); const status = await _dali.backend.status(); if (! status.logged_in) { lib_plankton.log.error( "dali.oidc_login_failed" ); await _dali.notify_logout(); return Promise.reject(new Error("oidc login failed")); } else { await _dali.notify_login(status.name); return Promise.resolve(undefined); } } /** */ export async function logout( ) : Promise { try { await _dali.backend.session_end( ); notify_logout(); } catch (error) { lib_plankton.log.notice( "dali.logout_failed", { "reason": String(error), } ); } } }