Revert "fix(server): redirect to setup page if not initialized (#7871)"

This reverts commit 42b5ef7a8b.
This commit is contained in:
forehalo
2024-08-14 16:37:52 +08:00
parent 42b5ef7a8b
commit cd3924b8fc
2 changed files with 8 additions and 54 deletions

View File

@@ -1,42 +1,9 @@
import { Injectable, Module, NestMiddleware } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import type { Request, Response } from 'express';
import { Module } from '@nestjs/common';
import { AuthModule } from '../auth';
import { UserModule } from '../user';
import { CustomSetupController } from './controller';
@Injectable()
export class SetupMiddleware implements NestMiddleware {
private initialized: boolean | null = null;
constructor(private readonly db: PrismaClient) {}
use(req: Request, res: Response, next: (error?: Error | any) => void) {
// never throw
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.allow().then(allowed => {
if (allowed) {
next();
} else if (!req.path.startsWith('/admin/setup')) {
res.redirect('/admin/setup');
}
});
}
async allow() {
try {
if (this.initialized === null) {
this.initialized = (await this.db.user.count()) > 0;
}
} catch (e) {
// avoid block the whole app
return true;
}
return this.initialized;
}
}
@Module({
imports: [AuthModule, UserModule],
controllers: [CustomSetupController],