lib/users.js
function create (client) {
  return function create (realm, user) {
    return new Promise((resolve, reject) => {
      const req = {
        url: `${client.baseUrl}/admin/realms/${realm}/users`,
        auth: {
          bearer: privates.get(client).accessToken
        },
        body: user,
        method: 'POST',
        json: true
      };

      request(req, (err, resp, body) => {
        if (err) {
          return reject(err);
        }

        if (resp.statusCode !== 201) {
          return reject(body);
        }

        // eg "location":"https://<url>/auth/admin/realms/<realm>/users/499b7073-fe1f-4b7a-a8ab-f401d9b6b8ec"
        const uid = resp.headers.location.replace(/.*\/(.*)$/, '$1');

        // Since the create Endpoint returns an empty body, go get what we just imported.
        // *** Body is empty but location header contains user id ***
        // We need to search based on the userid, since it will be unique
        return resolve(client.users.find(realm, {
          userId: uid
        }));
      });
    });
  };
}
function create (client) {
  return function create (realm, user) {
    return new Promise((resolve, reject) => {
      const req = {
        url: `${client.baseUrl}/admin/realms/${realm}/users`,
        auth: {
          bearer: privates.get(client).accessToken
        },
        body: user,
        method: 'POST',
        json: true
      };

      request(req, (err, resp, body) => {
        if (err) {
          return reject(err);
        }

        if (resp.statusCode !== 201) {
          return reject(body);
        }

        // eg "location":"https://<url>/auth/admin/realms/<realm>/users/499b7073-fe1f-4b7a-a8ab-f401d9b6b8ec"
        const uid = resp.headers.location.replace(/.*\/(.*)$/, '$1');

        // Since the create Endpoint returns an empty body, go get what we just imported.
        // *** Body is empty but location header contains user id ***
        // We need to search based on the userid, since it will be unique
        return resolve(client.users.find(realm, {
          userId: uid
        }));
      });
    });
  };
}
function create ( client ) { return function create ( realm , user ) { return new Promise ( ( resolve , reject ) => { const req = { url : ` ${ client . baseUrl } ${ realm } ` , auth : { bearer : privates . get ( client ) . accessToken } , body : user , method : 'POST' , json : true } ; request ( req , ( err , resp , body ) => { if ( err ) { return reject ( err ) ; } if ( resp . statusCode !== 201 ) { return reject ( body ) ; } // eg "location":"https://<url>/auth/admin/realms/<realm>/users/499b7073-fe1f-4b7a-a8ab-f401d9b6b8ec" const uid = resp . headers . location . replace ( / .*\/(.*)$ / , '$1' ) ; // Since the create Endpoint returns an empty body, go get what we just imported. // *** Body is empty but location header contains user id *** // We need to search based on the userid, since it will be unique return resolve ( client . users . find ( realm , { userId : uid } ) ) ; } ) ; } ) ; } ; }
A function to create a new user for a realm.
@param {string} realmName - The name of the realm(not the realmID) - ex: master
@param {object} user - The JSON representation of a user - http://keycloak.github.io/docs/rest-api/index.html#_userrepresentation - username must be unique
@returns {Promise} A promise that will resolve with the user object
@example
keycloakAdminClient(settings)
.then((client) => {
client.users.create(realmName, user)
.then((createdUser) => {
console.log(createdUser) // [{...}]
})
})
A function to create a new user for a realm .