This is a sample project to test Auth0 integration with NestJS for creation of a plugin.
$ npm install
Follow the instructions in the individual README.md files in the Admin-UI and Server folders.
- Auth0 account
- Create an Auth0 application ( Single Page Application )
- Create an Auth0 API ( Machine to Machine )
- Create an Auth0 action ( Post Login ) to add the user's name, email in access token
- Create an admin user in the postgres database made through the docker-compose file with sample data as shown in the swagger documentation of the server project. Add same email and name as the one to be used during login in Auth0. Sample
-
Go to Auth0 and create an account.
-
Create a new application of type Single Page Application ( Select the React Quickstart )
-
Change the following values to their keys :-
- Allowed Callback URLs : http://localhost:3001/auth-callback
- Allowed Logout URLs : http://localhost:3001/login
- Allowed Web Origins : http://localhost:3001
-
Update the Environment Variables in the admin-ui project with the values of the Auth0 application created :-
REACT_APP_AUTH0_DOMAIN = YOUR_AUTH0_DOMAIN; REACT_APP_AUTH0_CLIENT_ID = YOUR_AUTH0_CLIENT_ID;
-
Go to the dashboard and create a new API of type Machine to Machine
-
Set the following values :-
- Identifier : https://sample-nest.demo.com
- Signing Algorithm : RS256
-
Update the Environment Variables in the server project with the values of the Auth0 API created :-
AUTH0_AUDIENCE = YOUR_AUTH0_API_IDENTIFIER; // https://sample-nest.demo.com AUTH0_ISSUER = YOUR_AUTH0_DOMAIN; // Look at quickstart for the format
-
Go to the dashboard and create a new action of type Post Login through Flows tab ----> Login -----> Create Action
-
Set the following values :-
-
Name : Add user details to access token
-
Code :-
exports.onExecutePostLogin = async (event, api) => { if (event.authorization) { // Set claims api.accessToken.setCustomClaim("user", event.user); } };
-
-
Save and deploy the action
-
Put the action in the Login flow by going to Flows tab ----> Login -----> Select the action from the dropdown and put it between Login and Token issued.
-
Go to CustomSeed and add user details as follows :-
const user = { name: "YOUR_NAME", email: "YOUR_EMAIL", roles: ["user"], // TODO: Add a default role username: "YOUR_USERNAME" age: 0, birthDate: new Date(), score: 0, interests: [], priority: "low", isCurious: false, location: "(32.085300, 34.781769)", bio: "YOUR BIO", extendedProperties: {}, };
-
Then run the following command to seed the database :-
$ npm run seed
-
The user can now login with the email in Auth0.