1.0.1 • Published 3 years ago

@jimuelpalaca/nestjs-passport-firebase v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Installation

Install the following peer dependencies:

npm install passport @nestjs/passport passport-jwt jwks-rsa
npm install --save-dev @types/passport-jwt

Install the package

npm install @jimuelpalaca/nestjs-passport-firebase

Usage

Setup FirebaseAuthModule

Import the FirebaseAuthModule into the root module (the AppModule, defined in the app.module.ts file).

import { Module } from '@nestjs/common';
import { FirebaseAuthModule } from '@jimuelpalaca/nestjs-passport-firebase';

@Module({
  imports: [
     FirebaseAuthModule.register({
        audience: '<PROJECT_ID>',
        issuer: 'https://securetoken.google.com/<PROJECT_ID>',
     }),
  ],
})
export class AppModule {}

The value of audience is a string equal to your Firebase project ID, the unique identifier for your Firebase project. For the issuer it should be set to https://securetoken.google.com/<PROJECT_ID>. You can also store this config to the environment variable.

FirebaseAuthModule.register({
    audience: process.env.FIREBASE_AUDIENCE, 
    issuer: proccess.env.FIREBASE_ISSUER,
})

Protect your APIs

Use FirebaseAuthGuard to protect your routes.

import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { FirebaseAuthGuard } from '@jimuelpalaca/nestjs-passport-firebase';

@Controller()
export class AppController {
    constructor(private readonly appService: AppService) {}

    @Get()
    @UseGuards(FirebaseAuthGuard)
    getHello(): string {
        return this.appService.getHello();
    }
}

If you are using GraphQL, you need to extend the FirebaseAuthGuard and override the getRequest() method. Read more here.

import { ExecutionContext, Injectable } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { FirebaseAuthGuard } from '@jimuelpalaca/nestjs-passport-firebase';

@Injectable()
export class GqlAuthGuard extends FirebaseAuthGuard {
    getRequest(context: ExecutionContext) {
        const ctx = GqlExecutionContext.create(context);
        return ctx.getContext().req;
    }
}

You can now protect your queries and mutations by using the GqlAuthGuard.

import { Query, Resolver } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from './guards/gql-auth.guard';

@Resolver()
export class VersionsResolver {
    constructor(private readonly appService: AppService) {}

    @Query(() => String)
    @UseGuards(GqlAuthGuard)
    getHello(): string {
        return this.appService.getHello();
    }
}

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Author

Jimuel Palaca

License

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