15 lines
558 B
TypeScript
15 lines
558 B
TypeScript
import Auth from '../model/auth';
|
|
import { AuthModel, AuthPrivate } from '../schema/auth';
|
|
import { sign } from './jwt';
|
|
|
|
export const getAuthenticationBundle = async (username: string, password: string) => {
|
|
const auth = await Auth.findByUsername(username).catch();
|
|
const isAuthenticated = !!auth && (auth as AuthModel).authenticate(password);
|
|
const record = isAuthenticated ? (auth as AuthPrivate).record : null;
|
|
const token = isAuthenticated ? (auth as AuthModel).getToken() : sign();
|
|
return {
|
|
record,
|
|
token,
|
|
};
|
|
};
|