33 lines
836 B
TypeScript
33 lines
836 B
TypeScript
import { compare } from 'bcrypt';
|
|
import * as fs from 'fs/promises';
|
|
import path from 'path';
|
|
import { User } from './models/managed';
|
|
|
|
export class Managed {
|
|
private accounts: User[] = [];
|
|
|
|
async load(): Promise<void> {
|
|
const file = path.join(__dirname, '..', 'managed.json');
|
|
let content = '[]';
|
|
try {
|
|
content = await fs.readFile(file, { encoding: 'utf-8' });
|
|
} catch (e: unknown) {
|
|
if ((e as Error).message.includes('ENOENT')) {
|
|
fs.writeFile(file, '[]');
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
this.accounts = JSON.parse(content);
|
|
}
|
|
|
|
getAccount(name: string): User | undefined {
|
|
return this.accounts.find((item) => item.name === name);
|
|
}
|
|
|
|
async validatePassword(account: User, password: string): Promise<boolean> {
|
|
return compare(password, account.password);
|
|
}
|
|
}
|