@auth0/actions NPM package is the official Actions library that facilitates the Auth0 Actions TypeScript definitions. This helps you code and test your project’s Actions in external editors and IDEs.
Benefits
This library helps you with the following use cases:- IDE / Code Editor Assistance: By referencing this library, IDEs and code editors can help developers coding with autocompletion, object and function definitions, and error checking.
- TypeScript Development: Although Actions are still coded and work using Node.js CommonJS, this library enables Actions development on external projects using TypeScript which then can be built and deployed as Common JS to the Auth0 Tenant.
- Unit Testing Improvements: By enabling TypeScript development on external projects, this library allows developers to follow best practices and to improve their Unit Testing based on the TypeScript definitions.
- AI Actions Generation: This library gets us one step closer for AI to be able to generate more accurate Action examples.
How It Works
Installation
Use one of the following package managers to install the package as a development dependency:The package must be used as a development dependency to supplement your development tools.
- NPM:
npm install @auth0/actions --save-dev - Yarn:
yarn add @auth0/actions --dev - Pnpm:
pnpm add @auth0/actions --save-dev
Import
The library has the following structure:@auth0/actions/[trigger_name]/[trigger_version]
For example: @auth0/actions/post-login/v3
Use one of the following alternatives to import the TypeScript definitions into your Actions depending on your technology:
Use this alternative when you want IntelliSense without changing your existing JavaScript code structure:
- JSDoc @import
- JSDoc @param
- TypeScript import
Use this alternative when you want IntelliSense without changing your existing JavaScript code structure:
When using TypeScript, you must compile your code to JavaScript before deploying to Auth0. The Auth0 Actions runtime only executes JavaScript.Use the TypeScript compiler (
tsc) to transpile your .ts files to .js files, before it can be deployed. You must also include JSDoc comments to enable IntelliSense in the Dashboard.Examples
The following Actions examples are intentionally presented in both JavaScript and TypeScript to provide a direct, side-by-side comparison.Configuration
- JavaScript
- TypeScript
In your
package.json, define any development dependencies to have intelliSense help when writing your Action:Post-Login access control and ID token custom claims
The following example Action would execute during the Post-Login flow. It checks if the user has roles assigned, and callsapi.access.deny() if none are found. If roles are present, it proceeds to set the custom claim on the ID token.
The import statement declares the availability of external types to your code. This allows the editor to know the structure of the event and api objects.
- JavaScript
- TypeScript