- CodeWise: Changed everything to use undefined when not present - CodeWise: checks for nonpresent value using `isNullish` - enableOtp: enable Otp, return topt url. Does nothing when already enabled - disableOtp: disable 2FA Totp for the user - statusOtp: get the 2FA status for the user. return the Totp Url if enabled - loginDemo: split into two files - loginDemo: supports for 2FA - loginDemo: better response box
38 lines
987 B
TypeScript
38 lines
987 B
TypeScript
import fp from 'fastify-plugin'
|
|
import { FastifyInstance, FastifyPluginAsync } from 'fastify'
|
|
|
|
import { Database as DbImpl } from "./mixin/_base";
|
|
import { UserImpl, IUserDb } from "./mixin/user";
|
|
import { isNullish } from '@shared/utils';
|
|
|
|
|
|
Object.assign(DbImpl.prototype, UserImpl);
|
|
|
|
export interface Database extends DbImpl, IUserDb { }
|
|
|
|
// When using .decorate you have to specify added properties for Typescript
|
|
declare module 'fastify' {
|
|
export interface FastifyInstance {
|
|
db: Database;
|
|
}
|
|
}
|
|
|
|
let dbAdded = false;
|
|
|
|
export const useDatabase = fp<FastifyPluginAsync>(async function(
|
|
f: FastifyInstance,
|
|
_options: {}) {
|
|
if (dbAdded)
|
|
return;
|
|
dbAdded = true;
|
|
let path = process.env.DATABASE_DIR;
|
|
if (isNullish(path))
|
|
throw "env `DATABASE_DIR` not defined";
|
|
f.log.info(`Opening database with path: ${path}/database.db`)
|
|
let db: Database = new DbImpl(`${path}/database.db`) as Database;
|
|
if (!f.hasDecorator("db"))
|
|
f.decorate('db', db);
|
|
});
|
|
|
|
export default useDatabase;
|
|
|