NestJS middleware to verify tokens from Firebase.
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { FirebaseAuthMiddleware } from 'nestjs-firebase-auth-middleware';
import { AppController } from './app.controller';
@Module({
imports: [],
controllers: [AppController],
providers: [],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(FirebaseAuthMiddleware)
.forRoutes(AppController);
}
}
Once the token is validated, the user data will be added to the request. You can access it like so:
import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';
@Controller()
export class AppController {
constructor() {}
@Get()
index(@Req() request: Request) {
console.log(request.user);
}
}