fixes(eslint): fixing everything that eslint complained about
This commit is contained in:
parent
d11e4a4516
commit
404735fe22
17 changed files with 1566 additions and 69 deletions
|
|
@ -26,11 +26,16 @@ declare module 'fastify' {
|
|||
export interface FastifyContextConfig {
|
||||
requireAuth?: boolean;
|
||||
}
|
||||
export interface RouteOptions {
|
||||
[kRouteAuthDone]: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export const Otp = OTP;
|
||||
let jwtAdded = false;
|
||||
export const jwtPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
|
||||
void _opts;
|
||||
|
||||
if (jwtAdded) return;
|
||||
jwtAdded = true;
|
||||
const env = process.env.JWT_SECRET;
|
||||
|
|
@ -65,16 +70,18 @@ export type JwtType = Static<typeof JwtType>;
|
|||
|
||||
let authAdded = false;
|
||||
export const authPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
|
||||
void _opts;
|
||||
|
||||
if (authAdded) return void console.log('skipping');
|
||||
authAdded = true;
|
||||
await fastify.register(useDatabase as any, {});
|
||||
await fastify.register(jwtPlugin as any, {});
|
||||
await fastify.register(useDatabase as FastifyPluginAsync, {});
|
||||
await fastify.register(jwtPlugin as FastifyPluginAsync, {});
|
||||
await fastify.register(cookie);
|
||||
if (!fastify.hasRequestDecorator('authUser')) {fastify.decorateRequest('authUser', undefined);}
|
||||
if (!fastify.hasRequestDecorator('authUser')) { fastify.decorateRequest('authUser', undefined); }
|
||||
fastify.addHook('onRoute', (routeOpts) => {
|
||||
if (
|
||||
routeOpts.config?.requireAuth &&
|
||||
!(routeOpts as any)[kRouteAuthDone]
|
||||
!routeOpts[kRouteAuthDone]
|
||||
) {
|
||||
const f: preValidationAsyncHookHandler = async function(req, res) {
|
||||
try {
|
||||
|
|
@ -119,7 +126,7 @@ export const authPlugin = fp<FastifyPluginAsync>(async (fastify, _opts) => {
|
|||
routeOpts.preValidation = [routeOpts.preValidation, f];
|
||||
}
|
||||
|
||||
(routeOpts as any)[kRouteAuthDone] = true;
|
||||
routeOpts[kRouteAuthDone] = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -21,14 +21,15 @@ let dbAdded = false;
|
|||
|
||||
export const useDatabase = fp<FastifyPluginAsync>(async function(
|
||||
f: FastifyInstance,
|
||||
_options: {}) {
|
||||
if (dbAdded) {return;}
|
||||
_options: object) {
|
||||
void _options;
|
||||
if (dbAdded) { return; }
|
||||
dbAdded = true;
|
||||
const path = process.env.DATABASE_DIR;
|
||||
if (isNullish(path)) {throw 'env `DATABASE_DIR` not defined';}
|
||||
if (isNullish(path)) { throw 'env `DATABASE_DIR` not defined'; }
|
||||
f.log.info(`Opening database with path: ${path}/database.db`);
|
||||
const db: Database = new DbImpl(`${path}/database.db`) as Database;
|
||||
if (!f.hasDecorator('db')) {f.decorate('db', db);}
|
||||
if (!f.hasDecorator('db')) { f.decorate('db', db); }
|
||||
});
|
||||
|
||||
export default useDatabase;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import sqlite from 'better-sqlite3';
|
||||
|
||||
// @ts-ignore: this file is included using vite, typescript doesn't know how to include this...
|
||||
// @ts-expect-error: this file is included using vite, typescript doesn't know how to include this...
|
||||
import initSql from '../init.sql?raw';
|
||||
|
||||
export type SqliteReturn = object | undefined;
|
||||
|
|
|
|||
|
|
@ -94,17 +94,17 @@ export const UserImpl: Omit<IUserDb, keyof Database> = {
|
|||
},
|
||||
|
||||
getUserOtpSecret(this: IUserDb, id: UserId): string | undefined {
|
||||
const otp: any = this.prepare('SELECT otp FROM user WHERE id = @id LIMIT 1').get({ id }) as SqliteReturn;
|
||||
const otp = this.prepare('SELECT otp FROM user WHERE id = @id LIMIT 1').get({ id }) as ({ otp: string } | null | undefined);
|
||||
if (isNullish(otp?.otp)) return undefined;
|
||||
return otp.otp;
|
||||
},
|
||||
|
||||
ensureUserOtpSecret(this: IUserDb, id: UserId): string | undefined {
|
||||
const otp = this.getUserOtpSecret(id);
|
||||
if (!isNullish(otp)) {return otp;}
|
||||
if (!isNullish(otp)) { return otp; }
|
||||
const otpGen = new Otp();
|
||||
const res: any = this.prepare('UPDATE OR IGNORE user SET otp = @otp WHERE id = @id RETURNING otp')
|
||||
.get({ id, otp: otpGen.secret });
|
||||
const res = this.prepare('UPDATE OR IGNORE user SET otp = @otp WHERE id = @id RETURNING otp')
|
||||
.get({ id, otp: otpGen.secret }) as ({ otp: string } | null | undefined);
|
||||
return res?.otp;
|
||||
},
|
||||
|
||||
|
|
@ -154,12 +154,14 @@ async function hashPassword(
|
|||
*
|
||||
* @returns The user if it exists, undefined otherwise
|
||||
*/
|
||||
function userFromRow(row: any): User | undefined {
|
||||
function userFromRow(row: Partial<User>): User | undefined {
|
||||
if (isNullish(row)) return undefined;
|
||||
if (isNullish(row.id)) return undefined;
|
||||
if (isNullish(row.name)) return undefined;
|
||||
return {
|
||||
id: row.id as UserId,
|
||||
name: row.name || undefined,
|
||||
password: row.password || undefined,
|
||||
otp: row.otp || undefined,
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
password: row.password ?? undefined,
|
||||
otp: row.otp ?? undefined,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Type } from '@sinclair/typebox';
|
||||
import { TObject, TProperties, Type } from '@sinclair/typebox';
|
||||
|
||||
/**
|
||||
* @description Represent a message key
|
||||
|
|
@ -12,7 +12,7 @@ import { Type } from '@sinclair/typebox';
|
|||
* @example `pong.you.lost`
|
||||
*/
|
||||
export type MessageKey = string;
|
||||
export type ResponseBase<T = {}> = {
|
||||
export type ResponseBase<T = object> = {
|
||||
kind: string,
|
||||
msg: MessageKey,
|
||||
payload?: T,
|
||||
|
|
@ -26,7 +26,7 @@ export type ResponseBase<T = {}> = {
|
|||
* @example makeResponse("failure", "login.failure.invalid")
|
||||
* @example makeResponse("success", "login.success", { token: "supersecrettoken" })
|
||||
*/
|
||||
export function makeResponse<T = {}>(kind: string, key: MessageKey, payload?: T): ResponseBase<T> {
|
||||
export function makeResponse<T = object>(kind: string, key: MessageKey, payload?: T): ResponseBase<T> {
|
||||
console.log(`making response {kind: ${JSON.stringify(kind)}; key: ${JSON.stringify(key)}}`);
|
||||
return { kind, msg: key, payload };
|
||||
}
|
||||
|
|
@ -39,7 +39,7 @@ export function makeResponse<T = {}>(kind: string, key: MessageKey, payload?: T)
|
|||
* @example typeResponse("otpRequired", "login.otpRequired", { token: Type.String() })
|
||||
* @example typeResponse("success", "login.success", { token: Type.String() })
|
||||
*/
|
||||
export function typeResponse(kind: string, key: MessageKey | MessageKey[], payload?: any): any {
|
||||
export function typeResponse(kind: string, key: MessageKey | MessageKey[], payload?: TProperties): TObject<TProperties> {
|
||||
let tKey;
|
||||
if (key instanceof Array) {
|
||||
tKey = Type.Union(key.map(l => Type.Const(l)));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue