Initial auth library commmit
This commit is contained in:
14
lib/utils/auth.ts
Normal file
14
lib/utils/auth.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import Auth from '../model/auth';
|
||||
import { AuthDocument, 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 token = isAuthenticated ? (auth as AuthModel).getToken() : sign();
|
||||
const record = isAuthenticated ? (auth as AuthPrivate).record : null;
|
||||
return {
|
||||
record,
|
||||
token,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user