frontend-dali/source/base.ts
2025-10-28 11:38:58 +01:00

149 lines
2.4 KiB
TypeScript

/*
This file is part of »dali«.
Copyright 2025 'kcf' <fenris@folksprak.org>
»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 <http://www.gnu.org/licenses/>.
*/
namespace _dali
{
/**
*/
let _actions_login : Array<((name ?: (null | string)) => Promise<void>)> = [];
/**
*/
let _actions_logout : Array<(() => Promise<void>)> = [];
/**
*/
let _is_logged_in : boolean = false;
/**
*/
export function listen_login(
action : ((name ?: (null | string)) => Promise<void>)
)
: void
{
_actions_login.push(action);
}
/**
*/
export function listen_logout(
action : (() => Promise<void>)
)
: void
{
_actions_logout.push(action);
}
/**
*/
export async function notify_login(
name : (null | string)
)
: Promise<void>
{
_is_logged_in = true;
for (const action of _actions_login)
{
await action(name);
}
}
/**
*/
export async function notify_logout(
)
: Promise<void>
{
_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<void>
{
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<void>(new Error("oidc login failed"));
}
else
{
await _dali.notify_login(status.name);
return Promise.resolve<void>(undefined);
}
}
/**
*/
export async function logout(
)
: Promise<void>
{
try
{
await _dali.backend.session_end(
);
notify_logout();
}
catch (error)
{
lib_plankton.log.notice(
"dali.logout_failed",
{
"reason": String(error),
}
);
}
}
}