1.3.1 • Published 2 years ago

nestjs-auth0 v1.3.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Table Of Contents

About

nestjs-auth0 implements a module, Auth0AuthenticationModule, which when imported into your nestjs project provides a Auth0 AuthenticationClient to any class that injects it. This lets Auth0 be worked into your dependency injection workflow without having to do any extra work outside of the initial setup.

Installation

npm install --save auth0 nestjs-auth0

Getting Started

The simplest way to use nestjs-auth0 is to use Auth0AuthenticationModule.forRoot

import { Module } from '@nestjs-common';
import { Auth0AuthenticationModule } from 'nestjs-auth0';

@Module({
  imports: [
    Auth0AuthenticationModule.forRoot({
      domain: '',
      clientId: '',
      clientSecret: '',
    }),
  ],
})
export class AppModule {}

You can then inject the Auth0 AuthenticationClient into any of your injectables by using a custom decorator

import { Injectable } from '@nestjs/common';
import { InjectAuthenticationClient } from 'nestjs-auth0';
import { AuthenticationClient } from 'auth0';

@Injectable()
export class AppService {
  public constructor(@InjectAuthenticationClient() private readonly authenticationClient: AuthenticationClient) {}
}

Asynchronous setup is also supported

import { Module } from '@nestjs-common';
import { Auth0AuthenticationModule } from 'nestjs-auth0';

@Module({
  imports: [
    Auth0AuthenticationModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        domain: configService.get('auth0_domain'),
        clientId: configService.get('auth0_client_id'),
        clientSecret: configService.get('auth0_client_secret'),
      }),
    }),
  ],
})
export class AppModule {}

Example

In order to run the example run the following commands in your terminal. The expected output of the example is to show that the Auth0 AuthenticationClient was successfully injected into the AppService.

cd example
npm install
npm run start

Contributing

I would greatly appreciate any contributions to make this project better. Please make sure to follow the below guidelines before getting your hands dirty.

  1. Fork the repository
  2. Create your branch (git checkout -b my-branch)
  3. Commit any changes to your branch
  4. Push your changes to your remote branch
  5. Open a pull request

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

Copyright © 2022 Hoàng Nguyễn