how create user in nestjs using auth0 api

Viewed 46

i tried to create user in auth0 api with nestjs but give me error with status 401 enter image description here

my code is:enter code here

 @Post()
  async create( @Headers() authorization: string, @Body() User: any) {
    try {
      // @ts-ignore
      const response = await axios.post(process.env.AUTH0_USER_CREATE, authorization, User);
      // tslint:disable-next-line:no-console
      console.log( authorization, User);
      return response;
    } catch (error) {
      // tslint:disable-next-line:no-console
      console.log( error, authorization, User);
      return error;
    }

  }
1 Answers

Based on the error code it appears to be an authentication issue. As @Jay McDoniel stated, make sure that you are providing arguments to axios.post in the proper order and credentials are valid. Also it might be worthwhile to check the Auth0 logs.

I suggest that you should look into the Node Auth0 library for handling these operations

Example

import { ManagementClient } from 'auth0';

// Authenticating with the Auth0 API
const auth0 = new ManagementClient({
  domain: '{YOUR_ACCOUNT}.auth0.com',
  clientId: '{YOUR_NON_INTERACTIVE_CLIENT_ID}',
  clientSecret: '{YOUR_NON_INTERACTIVE_CLIENT_SECRET}'
});

// Creating a new user
const auth0User = await auth0.createUser(User);
Related