Cannot create new users

I amhaving considerable issues getting a seemingly basic operation to work. I don’t know why this is made so difficult, or why the docs are so disparate but here we are…

I have an endpoint that creates basic users on registration, so that when we launch they don’t have to sign up or anything like that. I am getting the access token from the management API after having created another machine-to-machine application. I am then passing that token into another request object to create the user.

I am getting 401 from the axios request and “invalid URI” from the request request.

How can I make this work?

var request = require("request");
const axios = require("axios");
var qs = require('qs');
var tokenOptions = { method: 'POST',

    url: 'https://<dev>.au.auth0.com/oauth/token',
    headers: { 'content-type': 'application/json' },
    body: '{"client_id":"<id>","client_secret":"<secre>","audience":"https://<sub>.au.auth0.com/api/v2/","grant_type":"client_credentials"}'

};

request(tokenOptions, function (tokenError, response, tokenbody) {

    if (tokenError) {
        throw new Error(tokenError);
    }

    const bodyJson = JSON.parse(tokenbody);
    console.log(bodyJson);
    var createClientOptions = { method: 'POST',
        url: 'https://<sub>.au.auth0.com/api/v2/users',
        headers: {
            'Content-Type': 'application/json',
            'authorization': `Bearer ${bodyJson.access_token}`,
        },
        body: {
            "email": "<email>@gmail.com",
            "blocked": false,
            "email_verified": false,
            "connection": "Initial-Connection",
            "password": "secret",
            "verify_email": true,
            "username": "johndoe",
        },
    }

    request(JSON.stringify(createClientOptions), function(Error, some, createClientResponse){
        console.log(Error);
        console.log(some);
        console.log(createClientResponse);
    })

 
    axios.post('https://<sub>.auth0.com/api/v2/users', qs.stringify(createClientOptions))
    .then(createClientResponse => {
        console.log(createClientResponse.data);
    })
    .catch(error => {
        console.log(error);
    });

});

Hi @aus-it-orders,

Have considered using our auth0 node library? The management client makes it dead simple. I would recommend it if you are having trouble making your own requests against the management API.

I am curious, why are using using both axios and request ?

Here is a code snippet that should work.

const ManagementClient = require('auth0').ManagementClient;
const auth0 = new ManagementClient({
  domain: '{YOUR_ACCOUNT}.auth0.com',
  clientId: '{YOUR_NON_INTERACTIVE_CLIENT_ID}',
  clientSecret: '{YOUR_NON_INTERACTIVE_CLIENT_SECRET}',
  scope: 'update:users'
});

const data = {
  "email": "<email>@gmail.com",
  "blocked": false,
  "email_verified": false,
  "connection": "Initial-Connection",
  "password": "secret",
  "verify_email": true,
  "username": "johndoe"
}
  
auth0.createUser(data, function (err) {
  if (err) {
    // Handle error.
  console.log(err)
  }

  // User created.
});

Hi Dan,

Thanks for your response.

I had thought of using that but tbh the docs are not very clear about what the difference is between the two methods and how to use Auth0 in this way. Now I see this is obviously the better and preferred option.

There seem to be two copies of the docs with one neatly organized in the link you have provided and the other smattered across tutorials and the getting started section on the website inside the accounts section. I suspect the link you provided is the new and improved documentation and I must say it is very nice indeed.

I’ve used request and axios because that is how the instructions are laid out in the application window on my Auth0 account, I too found that odd. See my previous comment about fractured documentation. I tried to use one or the other but had issues when not strictly following the documentation.

Thanks

If you can provide examples of the documentation issues I would be happy to pass the feedback along to our docs team.

Sure, in the “Quick Start” section of the Machine 2 Machine applications:

Then just below it:

Of course, the m2m application was created as a prerequisite to creating new users because I thought I would need to access the management API.

There is this documentation: Auth0 Management API v2
Then there is this documentation: Create Users


Anyway, all of that aside it turns out that what I really need was to simply change the login type to password-less and eliminate the need for any of the above. Again, something that ought to be front of mind in the docs as passwords are outmoded.

I found a solution at this article: Auth0 Passwordless Authentication | Medium

Thanks for the thorough feedback. We really appreciate it. I’ll take a look at this and pass along the relevant info to the teams who manage these code snippets. I agree, it’s odd that one uses request and one uses axios.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.