Skip to content

Vn-ChemGio/nestjs-auth0

Repository files navigation

NestJS Auth0

Test package

NodeJS Auth0 Management Client for Nestjs v10.x.x and last version of Auth0 include Typescript (v4.x)

Install

npm i '@vn.chemgio/nestjs-auth0' auth0

Authentication Client

Add below code into app.module.js file.

import { AuthenticationModule } from "@vn.chemgio/nestjs-auth0";

@Module({
  imports: [
    AuthenticationModule.forRoot({
      domain: '{YOUR_ACCOUNT}.auth0.com',
      clientId: '{CLIENT_ID}',
      clientSecret: '{CLIENT_SECRET}',
    }),
  ],
})
export class AppModule {}

Now you can inject authentication client into your services, for example:

import { Injectable } from '@nestjs/common';
import { InjectAuthentication } from "@vn.chemgio/nestjs-auth0";
import { AuthenticationClient, TokenResponse } from "auth0";

@Injectable()
export class AppService {
  constructor(@InjectAuthentication() private readonly authentication: AuthenticationClient) { }

  async getCredentialsGrant(): Promise<TokenResponse> {
    return await this.authentication.clientCredentialsGrant({ audience: "..." });
  }
}

Management Client

Add below code into app.module.js file.

import { ManagementModule } from "@vn.chemgio/nestjs-auth0";

@Module({
  imports: [
    ManagementModule.forRoot({
      token: '{YOUR_API_V2_TOKEN}',
      domain: '{YOUR_ACCOUNT}.auth0.com',
    }),
  ],
})
export class AppModule {}

Now you can inject management client into your services, for example:

import { Injectable } from "@nestjs/common";
import { InjectManagement } from "@vn.chemgio/nestjs-auth0";
import { ManagementClient, User } from "auth0";

@Injectable()
export class AppService {
  constructor(@InjectManagement() private readonly management: ManagementClient) { }

  async getUsers(): Promise<User[]> {
    return await this.management.getUsers();
  }
}

To obtain automatically a Management API token via the ManagementClient, you can specify the parameters clientId, clientSecret (use a Non Interactive Client) and optionally scope. Behind the scenes the Client Credentials Grant is used to obtain the access_token and is by default cached for the duration of the returned expires_in value.

import { ManagementModule } from "@vn.chemgio/nestjs-auth0";

@Module({
  imports: [
    ManagementModule.forRoot({
      domain: '{YOUR_ACCOUNT}.auth0.com',
      clientId: '{YOUR_NON_INTERACTIVE_CLIENT_ID}',
      clientSecret: '{YOUR_NON_INTERACTIVE_CLIENT_SECRET}',
      scope: 'read:users update:users',
    }),
  ],
})
export class AppModule {}

More details you can find here: auth0/node-auth0

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published