Files
auth-db/lib/api/authenticate.ts
2023-05-23 22:12:01 -04:00

18 lines
531 B
TypeScript

import { Auth, Log } from '..';
import { Action } from '../constants/action';
import { getLoginToken } from '../utils/getLoginToken';
export const authenticate = async (username: string, password: string) => {
const doc = await Auth.findByUsername(username).catch();
if (!!doc && (await doc.authenticate(password))) {
Log.add(doc.id, Action.AUTHENTICATE);
return { ...doc, token: getLoginToken(doc) };
}
if (doc) {
Log.add(doc.id, Action.AUTHENTICATE_FAILURE);
}
return false;
};