@twirelab/nestjs-auth0
v1.0.0
Published
Auth0 wrapper for Nestjs
Maintainers
Readme
NestJS Auth0
NodeJS Auth0 wrapper for Nestjs
Install
npm i @twirelab/nestjs-auth0 auth0or
yarn add @twirelab/nestjs-auth0 auth0Note: Starting from version 1.0.0, Auth0 v5 includes built-in TypeScript types, so you no longer need to install
@types/auth0separately.
Authentication Client
Add below code into app.module.js file.
import { AuthenticationModule } from "@twirelab/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 "@twirelab/nestjs-auth0";
import { AuthenticationClient } from "auth0";
@Injectable()
export class AppService {
constructor(
@InjectAuthentication()
private readonly authentication: AuthenticationClient
) {}
async signUp(email: string, password: string) {
return await this.authentication.database.signUp({
connection: "Username-Password-Authentication",
username: email,
password: password,
});
}
async login(email: string, password: string) {
return await this.authentication.database.signIn({
connection: "Username-Password-Authentication",
username: email,
password: password,
});
}
}Management Client
Add below code into app.module.js file.
import { ManagementModule } from "@twirelab/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 "@twirelab/nestjs-auth0";
import { ManagementClient } from "auth0";
@Injectable()
export class AppService {
constructor(
@InjectManagement() private readonly management: ManagementClient
) {}
async getUsers() {
return await this.management.users.list();
}
async createUser(userData: any) {
return await this.management.users.create(userData);
}
async getClients() {
return await this.management.clients.list();
}
}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 "@twirelab/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
Migration from 0.x to 1.0
This is a breaking change release that migrates from Auth0 v4 to v5 and NestJS 10 to 11. Here are the key changes:
Breaking Changes
- Auth0 v5 Migration: Updated from Auth0 v4 to v5
- NestJS v11 Migration: Updated from NestJS 10 to 11
- TypeScript Types: Auth0 v5 includes built-in TypeScript types - you no longer need
@types/auth0 - API Changes: Some method names and signatures have changed in Auth0 v5
- Peer Dependencies: Updated peer dependencies to require NestJS 11+
Migration Steps
Update dependencies:
npm uninstall @types/auth0 npm install @twirelab/nestjs-auth0@^1.0.0 auth0@^5.0.0 npm install @nestjs/common@^11.0.0 @nestjs/core@^11.0.0Update your code:
- Remove any imports of
@types/auth0 - Update method calls to match Auth0 v5 API (see Auth0 v5 Migration Guide)
- Update to NestJS 11 (see NestJS Migration Guide)
- The client injection and module configuration remain the same
- Remove any imports of
Test thoroughly: Ensure all Auth0 API calls work with the new v5 client and NestJS 11
What's New in v1.0
- ✅ Full Auth0 v5 support
- ✅ NestJS 11 compatibility
- ✅ Built-in TypeScript types (no more
@types/auth0) - ✅ Improved error handling
- ✅ Better performance
- ✅ Enhanced security features
- ✅ Comprehensive test coverage
- ✅ Updated peer dependencies for NestJS 11
