Skip to content

lucienbl/nestjs-role-based-auth-starter

Repository files navigation

NestJS Firebase Role Based Auth Starter

Features

  • Role based authentication with firebase 🔐
  • PostgreSQL with TypeORM 💾
  • Swagger 📚
  • Send mails via SMTP server 📧
  • FCM notifications ✉️

Good to know

Database version check

All entities that have a @VersionColumn (all by default) will be automatically version checked thanks to the OptimisticLockingSubscriber from here.

Route access control

Use the following decorators on your controller class :

@UseGuards(FirebaseAuthGuard, RolesGuard)
@RolesAllowed(Roles.ADMIN)

This will enable access control for all the routes within this controller and allow the ADMIN users to access all routes by default.

If you want for example to additionally allow USER users to access a specific route only you can use the same decorator on the route :

@RolesAllowed(Roles.USER)

To allow everyone to access a route and to bypass class-level restrictions on a specific route, you can use the @Public decorator.

Please see src/users/users.contoller.ts as an example.

Installation

  1. Install dependencies
$ yarn install
  1. Add the service-account.json (for firebase authentication) file at the root of the project.

  2. Create the .env file from .env.example and replace values.

  3. Setup database

$ docker-compose up -d

Running

# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod

Test

# unit tests
$ yarn run test

# e2e tests
$ yarn run test:e2e

# test coverage
$ yarn run test:cov