/nestjs-auth0-management

Primary LanguageTypeScriptMIT LicenseMIT

nestjs-auth0-management

Injectable Auth0 Management client for your nestjs projects

Using Auth0 Authentication, please see nestjs-auth0

Table Of Contents

About

nestjs-auth0-management implements a module, Auth0ManagementModule, 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-management

Getting Started

The simplest way to use nestjs-auth0-management is to use Auth0ManagementModule.forRoot

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

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

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

import { Injectable } from '@nestjs/common';
import { InjectManagementClient } from 'nestjs-auth-management';
import { ManagementClient } from 'auth0';

@Injectable()
export class AppService {
  public constructor(@InjectManagementClient() private readonly managementClient: ManagementClient) {}
}

Asynchronous setup is also supported

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

@Module({
  imports: [
    Auth0ManagementModule.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