Skip to content

speakbox/nestjs-firebase-admin

Repository files navigation

NestJS Firebase Admin

Installation

npm install @speakbox/nestjs-firebase-admin

Usage

Import the module and the service in your NestJS application:

import { Module } from '@nestjs/common';
import { FirebaseModule, FirebaseService } from '@speakbox/nestjs-firebase-admin';

@Module({
  imports: [FirebaseModule],
  providers: [FirebaseService],
})
export class AppModule {}

Using the service

import { Inject, Injectable } from "@nestjs/common";
import { FirebaseService } from '@speakbox/nestjs-firebase-admin';

@Injectable()
export class ExampleService {
  constructor(
    @Inject(FirebaseService) private readonly firebase: FirebaseService,
  ) {}
  
  exampleAuth() {
    return this.firebase.auth.getUser('userId');
  }
  
  exampleFirestore() {
    return this.firebase.firestore.collection('users').get();
  }
  
  exampleStorage() {
    return this.firebase.storage.bucket('bucketName').file('fileName').download();
  }
}

For more information on how to use the service, please refer to the Firebase Admin SDK documentation or the NestJS Documentation.

Using the middleware

import { MiddlewareConsumer, Module } from '@nestjs/common';
import { SomeController } from './some.controller';
import { FirebaseModule, FirebaseService, FirebaseMiddleware } from '@speakbox/nestjs-firebase-admin';

@Module({
  imports: [FirebaseModule],
  controllers: [SomeController],
  providers: [FirebaseService, FirebaseMiddleware],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(FirebaseModule).forRoutes(SomeController);
  }
}

For more information on how to use middlewares, see the NestJS documentation.

Environment variables

This package relies on environment variables to configure the Firebase Admin SDK:

# Application environment
# If set to "development", it will assume the use of the Firebase emulators (please see emulator related environment variables below)
# If set to anything else, it will assume usage inside a Google Cloud environment and attempt to use applicationDefault() credentials
APP_ENV=production

# ONLY USE THE FOLLOWING IN DEVELOPMENT
# Firebase emulator
# Set this variable in your .env if you want to use the Firestore emulator
FIRESTORE_EMULATOR_HOST=localhost:7979

# Firebase Auth emulator
# Set this variable in your .env if you want to use the Firebase Auth emulator
FIREBASE_AUTH_EMULATOR_HOST=localhost:9099

# Firebase Storage emulator
# Set this variable in your .env if you want to use the Firebase Storage emulator
STORAGE_EMULATOR_HOST=localhost:9199

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Author

Valentin Prugnaud @valentinprgnd

License

Licensed under the MIT License - see the LICENSE file for details.