-
-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NestJS][Fastify] Middleware doesn't work when using Fastify in NestJS #582
Comments
Update: a new version has been publish (10.0.0). However this did not fix the mentioned issue. A new issue has been created on NestJS's Github repo to address this. |
Update: the latest update for nestjs v10.1.3 still doesn't fix this issue :-( |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Can we reopen this issue? It has not been resolved yet. |
@DennisSnijder can you check if #704 solves this issue? |
This issue was not resolved and is still present. All other solutions mostly don't work. Current possible workaround is to wrap into a custom controller. Therefore it affects core functionality and probably should get resolved Node: Dependencies: |
I also had a hard time setting up the Auth middleware for the dashboard in my NestJS app with the Fastify adapter. At least I am glad now that someone mentioned it, and we all look forward to having this fixed soon. Any contribution here will be highly appreciated. |
Is there another way to set up authorization when using Fastify? I need to go into production soon and need to restrict access to the admin panel. |
For those of you looking for a workaround, here's roughly what you should do (please pardon any mistakes, you can not just copy-paste the code below but check out the import type { FastifyBasicAuthOptions } from '@fastify/basic-auth';
import basicAuth from '@fastify/basic-auth';
import { ConfigService } from '@nestjs/config';
import { BullModule as NestBullModule } from '@nestjs/bullmq';
import type { DynamicModule, NestModule } from '@nestjs/common';
import { HttpStatus, Module, UnauthorizedException } from '@nestjs/common';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
import { FastifyAdapter } from '@bull-board/fastify';
import { createBullBoard } from '@bull-board/api';
import { HttpAdapterHost } from '@nestjs/core';
import type { FastifyInstance } from 'fastify';
@Module({})
export class JobsModule implements NestModule {
constructor(
private readonly adapterHost: HttpAdapterHost,
@InjectQueue("QUEUE_NAME") private readonly queue: Queue,
) {}
static forRoot(): DynamicModule {
return {
module: QueueModule,
imports: [
BullModule,
BullModule.registerQueueAsync({
name: "QUEUE_NAME",
})
],
providers: [],
exports: [],
};
}
configure() {
const route = '/bullboard';
const serverAdapter = new FastifyAdapter();
serverAdapter.setBasePath(route);
createBullBoard({
queues: [
new BullMQAdapter(this.queue),
],
serverAdapter,
});
const app = this.adapterHost.httpAdapter.getInstance<FastifyInstance>();
// Basic auth
const authenticate: FastifyBasicAuthOptions['authenticate'] = true;
const validate: FastifyBasicAuthOptions['validate'] = async (
username,
password
) => {
if (username !== "YOUR_USERNAME" || password !== "YOUR_PASSWORD") {
throw new UnauthorizedException();
}
};
app.register(basicAuth, {
validate,
authenticate,
});
const bullboardPlugin = serverAdapter.registerPlugin();
/* Inspired by:
* https://github.com/felixmosh/bull-board/blob/master/examples/with-fastify-auth/basicAuth.js
* revisit later as the middleware isn't running correctly due to the following bug:
* https://github.com/nestjs/nest/issues/11802
*/
app.register(async (instance) => {
instance.addHook('onRequest', (req, reply, next) => {
instance.basicAuth(req, reply, function (error) {
if (!error) {
return next();
}
const statusCode =
(error as $TSFixMe).statusCode || HttpStatus.INTERNAL_SERVER_ERROR;
reply.code(statusCode).send({ error: error.name });
});
});
instance.register(bullboardPlugin, {
basePath: undefined,
prefix: route,
});
});
}
} |
@mareksuscak |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Context
Earlier on simplenotezy created the issue mentioning that middlewares are not being triggered when using the Fastify Adapter in combination with the NestJS module.
DennisSnijder/nestjs-bull-board#1
Since the issue was originally opened on the
nestjs-bull-board
github, I move the issue here since we migrated the package to@bull-board/nestjs
.I will keep this issue updated with updates regarding this issue and with a solution when it becomes available on NestJS's side.
What's the status?
The text was updated successfully, but these errors were encountered: