https://github.com/jimmycamus/nestjs-auth0
Auth0 integration for NestJS applications.
https://github.com/jimmycamus/nestjs-auth0
auth0 nestjs npm-package
Last synced: 5 months ago
JSON representation
Auth0 integration for NestJS applications.
- Host: GitHub
- URL: https://github.com/jimmycamus/nestjs-auth0
- Owner: JimmyCamus
- Created: 2025-09-09T01:39:01.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-09T02:41:18.000Z (10 months ago)
- Last Synced: 2025-09-27T00:44:21.084Z (9 months ago)
- Topics: auth0, nestjs, npm-package
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@jimmycamus/nestjs-auth0
- Size: 112 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
@jimmycamus/nestjs-auth0
Simple authentication and authorization for NestJS applications using Auth0. This package provides modules, decorators, and utilities to seamlessly integrate Auth0's OAuth2 flow into your controllers, handle callbacks, and access authenticated user data in a secure and transparent way. Ideal for projects that require social login, JWT, and user management with Auth0 in NestJS.
## Installation
```bash
pnpm add @jimmycamus/nestjs-auth0
```
## Basic Usage
```typescript
import { Module } from "@nestjs/common";
import { Auth0Module } from "@jimmycamus/nestjs-auth0";
@Module({
imports: [
Auth0Module.forRoot({
domain: "YOUR_AUTH0_DOMAIN",
clientId: "YOUR_AUTH0_CLIENT_ID",
clientSecret: "YOUR_AUTH0_CLIENT_SECRET",
redirectUri: "YOUR_AUTH0_REDIRECT_URI",
}),
],
})
export class AppModule {}
```
## Asynchronous Configuration
```typescript
Auth0Module.forRootAsync({
useFactory: async () => ({
domain: process.env.AUTH0_DOMAIN,
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
redirectUri: process.env.AUTH0_REDIRECT_URI,
}),
});
```
## Using Decorators
This package provides decorators to simplify Auth0 integration inside your NestJS controllers.
`@Auth0Login()`
Redirects the user to the Auth0 login page.
You don’t need to implement any logic in the handler — the interceptor will handle the redirection.
```typescript
import { Controller, Get } from "@nestjs/common";
import { Auth0Login } from "@jimmycamus/nestjs-auth0";
@Controller()
export class AppController {
@Get("authorize")
@Auth0Login()
authorize() {
// No logic needed here, user will be redirected to Auth0
}
}
```
`@Auth0Callback()`
Handles the Auth0 callback after login.
This decorator will:
- Process the Auth0 callback.
- Exchange the authorization code for an access token.
- Attach the authenticated user to the request object.
```typescript
import { Controller, Get, Req } from "@nestjs/common";
import { Request } from "express";
import { Auth0Callback, WithAuth0User } from "@jimmycamus/nestjs-auth0";
@Controller()
export class AppController {
@Get("callback")
@Auth0Callback()
callback(@Req() req: WithAuth0User) {
return { userData: req.user };
}
}
```
### Combined Example
```typescript
import { Controller, Get, Req } from "@nestjs/common";
import {
Auth0Callback,
Auth0Login,
WithAuth0User,
} from "@jimmycamus/nestjs-auth0";
import { Request } from "express";
@Controller()
export class ExampleController {
constructor() {}
@Get("authorize")
@Auth0Login()
authorize() {}
@Get("callback")
@Auth0Callback()
callback(@Req() req: WithAuth0User) {
return { userData: req.user };
}
}
```
## Contributing
Contributions are welcome!
Open an issue or a PR to suggest new features and improvements.
## License
MIT