backend/source/service-admin.ts

65 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-04-22 10:22:18 +02:00
namespace _espe.service.admin
2024-04-22 10:02:34 +02:00
{
/**
*/
export type type_value = {
name : string;
password_image : string;
};
/**
*/
function get(
name : string
) : (null | type_value)
{
2024-04-22 10:22:18 +02:00
const admins : Array<{name : string; password_image : string;}> = _espe.conf.get().admins;
2024-04-22 10:02:34 +02:00
const entry : (undefined | {name : string; password_image : string;}) = admins.find(entry_ => (entry_.name === name));
return (
(entry === undefined)
? null
: {
"name": entry.name,
"password_image": entry.password_image,
}
);
}
/**
*/
async function check_password(
value : type_value,
password_given : string
) : Promise<boolean>
{
2024-04-22 10:22:18 +02:00
return _espe.helpers.bcrypt_compare(value.password_image, password_given);
2024-04-22 10:02:34 +02:00
}
/**
*/
export async function login(
name : string,
password : string
) : Promise<(null | type_value)>
{
const value : (null | type_value) = get(name);
if (value === null) {
return null;
}
else {
if (! (await check_password(value, password))) {
return null;
}
else {
return value;
}
}
}
}