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
|
|
@ -1,17 +1,14 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
import fastifyFormBody from '@fastify/formbody';
|
||||
import fastifyMultipart from '@fastify/multipart';
|
||||
import { mkdir } from 'node:fs/promises';
|
||||
import fp from 'fastify-plugin';
|
||||
import * as db from '@shared/database';
|
||||
import * as auth from '@shared/auth';
|
||||
|
||||
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
|
||||
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
|
||||
|
||||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
export interface FastifyInstance {
|
||||
|
|
@ -19,20 +16,18 @@ declare module 'fastify' {
|
|||
}
|
||||
}
|
||||
|
||||
const app: FastifyPluginAsync = async (
|
||||
fastify,
|
||||
opts,
|
||||
): Promise<void> => {
|
||||
await fastify.register(db.useDatabase as any, {});
|
||||
await fastify.register(auth.jwtPlugin as any, {});
|
||||
await fastify.register(auth.authPlugin as any, {});
|
||||
const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
||||
void opts;
|
||||
await fastify.register(db.useDatabase as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.jwtPlugin as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.authPlugin as FastifyPluginAsync, {});
|
||||
|
||||
// Place here your custom code!
|
||||
for (const plugin of Object.values(plugins)) {
|
||||
void fastify.register(plugin as any, {});
|
||||
void fastify.register(plugin as FastifyPluginAsync, {});
|
||||
}
|
||||
for (const route of Object.values(routes)) {
|
||||
void fastify.register(route as any, {});
|
||||
void fastify.register(route as FastifyPluginAsync, {});
|
||||
}
|
||||
|
||||
void fastify.register(fastifyFormBody, {});
|
||||
|
|
|
|||
|
|
@ -12,10 +12,12 @@ export const WhoAmIRes = Type.Union([
|
|||
export type WhoAmIRes = Static<typeof WhoAmIRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.put(
|
||||
'/api/auth/disableOtp',
|
||||
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
|
||||
async function(req, _res) {
|
||||
void _res;
|
||||
if (isNullish(req.authUser)) {return makeResponse('failure', 'disableOtp.failure.generic');}
|
||||
this.db.deleteUserOtpSecret(req.authUser.id);
|
||||
return makeResponse('success', 'disableOtp.success');
|
||||
|
|
|
|||
|
|
@ -13,10 +13,12 @@ export const WhoAmIRes = Type.Union([
|
|||
export type WhoAmIRes = Static<typeof WhoAmIRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.put(
|
||||
'/api/auth/enableOtp',
|
||||
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
|
||||
async function(req, _res) {
|
||||
void _res;
|
||||
if (isNullish(req.authUser)) {return makeResponse('failure', 'enableOtp.failure.noUser');}
|
||||
const otpSecret = this.db.ensureUserOtpSecret(req.authUser!.id);
|
||||
if (isNullish(otpSecret)) {return makeResponse('failure', 'enableOtp.failure.noSecret');}
|
||||
|
|
|
|||
|
|
@ -21,10 +21,12 @@ export const LoginRes = Type.Union([
|
|||
export type LoginRes = Static<typeof LoginRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.post<{ Body: LoginReq; Response: LoginRes }>(
|
||||
'/api/auth/login',
|
||||
{ schema: { body: LoginReq, response: { '2xx': LoginRes } } },
|
||||
async function(req, _res) {
|
||||
void _res;
|
||||
try {
|
||||
const { name, password } = req.body;
|
||||
const user = this.db.getUserFromName(name);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.post(
|
||||
'/api/auth/logout',
|
||||
async function(_req, res) {
|
||||
void _req;
|
||||
return res.clearCookie('token').send('{}');
|
||||
},
|
||||
);
|
||||
|
|
|
|||
|
|
@ -21,30 +21,35 @@ type OtpRes = Static<typeof OtpRes>;
|
|||
const OTP_TOKEN_TIMEOUT_SEC = 120;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.post<{ Body: OtpReq }>(
|
||||
'/api/auth/otp',
|
||||
{ schema: { body: OtpReq, response: { '2xx': OtpRes } } },
|
||||
async function(req, _res) {
|
||||
void _res;
|
||||
try {
|
||||
const { token, code } = req.body;
|
||||
// lets try to decode+verify the jwt
|
||||
const dJwt = this.jwt.verify<JwtType>(token);
|
||||
|
||||
// is the jwt a valid `otp` jwt ?
|
||||
if (dJwt.kind != 'otp')
|
||||
// no ? fuck off then
|
||||
{return makeResponse('failed', 'otp.failed.invalid');}
|
||||
if (dJwt.kind != 'otp') {
|
||||
// no ? fuck off then
|
||||
return makeResponse('failed', 'otp.failed.invalid');
|
||||
}
|
||||
// is it too old ?
|
||||
if (dJwt.createdAt + OTP_TOKEN_TIMEOUT_SEC * 1000 < Date.now())
|
||||
// yes ? fuck off then, redo the password
|
||||
{return makeResponse('failed', 'otp.failed.timeout');}
|
||||
if (dJwt.createdAt + OTP_TOKEN_TIMEOUT_SEC * 1000 < Date.now()) {
|
||||
// yes ? fuck off then, redo the password
|
||||
return makeResponse('failed', 'otp.failed.timeout');
|
||||
}
|
||||
|
||||
// get the Otp sercret from the db
|
||||
const user = this.db.getUserFromName(dJwt.who);
|
||||
if (isNullish(user?.otp))
|
||||
// oops, either no user, or user without otpSecret
|
||||
// fuck off
|
||||
{return makeResponse('failed', 'otp.failed.noSecret');}
|
||||
if (isNullish(user?.otp)) {
|
||||
// oops, either no user, or user without otpSecret
|
||||
// fuck off
|
||||
return makeResponse('failed', 'otp.failed.noSecret');
|
||||
}
|
||||
|
||||
// good lets now verify the token you gave us is the correct one...
|
||||
const otpHandle = new Otp({ secret: user.otp });
|
||||
|
|
@ -58,10 +63,11 @@ const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
|||
];
|
||||
|
||||
// checking if any of the array match
|
||||
if (tokens.some((c) => c === code))
|
||||
// they do !
|
||||
// gg you are now logged in !
|
||||
{return makeResponse('success', 'otp.success', { token: this.signJwt('auth', dJwt.who) });}
|
||||
if (tokens.some((c) => c === code)) {
|
||||
// they do !
|
||||
// gg you are now logged in !
|
||||
return makeResponse('success', 'otp.success', { token: this.signJwt('auth', dJwt.who) });
|
||||
}
|
||||
}
|
||||
catch {
|
||||
return makeResponse('failed', 'otp.failed.generic');
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { FastifyPluginAsync } from 'fastify';
|
|||
import { Static, Type } from '@sinclair/typebox';
|
||||
import { typeResponse, makeResponse, isNullish } from '@shared/utils';
|
||||
|
||||
const USERNAME_CHECK: RegExp = /^[a-zA-Z\_0-9]+$/;
|
||||
const USERNAME_CHECK: RegExp = /^[a-zA-Z_0-9]+$/;
|
||||
|
||||
const SignInReq = Type.Object({
|
||||
name: Type.String(),
|
||||
|
|
@ -28,11 +28,13 @@ const SignInRes = Type.Union([
|
|||
|
||||
type SignInRes = Static<typeof SignInRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.post<{ Body: SignInReq }>(
|
||||
'/api/auth/signin',
|
||||
{ schema: { body: SignInReq, response: { '200': SignInRes, '5xx': Type.Object({}) } } },
|
||||
async function(req, res) {
|
||||
async function(req, _res) {
|
||||
void _res;
|
||||
const { name, password } = req.body;
|
||||
|
||||
if (name.length < 4) {return makeResponse('failed', 'signin.failed.username.tooshort');}
|
||||
|
|
|
|||
|
|
@ -14,10 +14,12 @@ export const StatusOtpRes = Type.Union([
|
|||
export type StatusOtpRes = Static<typeof StatusOtpRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.get(
|
||||
'/api/auth/statusOtp',
|
||||
{ schema: { response: { '2xx': StatusOtpRes } }, config: { requireAuth: true } },
|
||||
async function(req, _res) {
|
||||
void _res;
|
||||
if (isNullish(req.authUser)) {return makeResponse('failure', 'statusOtp.failure.generic');}
|
||||
const otpSecret = this.db.getUserOtpSecret(req.authUser.id);
|
||||
if (isNullish(otpSecret)) {return makeResponse('success', 'statusOtp.success.disabled');}
|
||||
|
|
|
|||
|
|
@ -12,10 +12,12 @@ export const WhoAmIRes = Type.Union([
|
|||
export type WhoAmIRes = Static<typeof WhoAmIRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
|
||||
void _opts;
|
||||
fastify.get(
|
||||
'/api/auth/whoami',
|
||||
{ schema: { response: { '2xx': WhoAmIRes } }, config: { requireAuth: true } },
|
||||
async function(req, _res) {
|
||||
void _res;
|
||||
if (isNullish(req.authUser)) {return makeResponse('failure', 'whoami.failure.generic');}
|
||||
return makeResponse('success', 'whoami.success', { name: req.authUser.name });
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue