Skip to content

Instantly share code, notes, and snippets.

@edewit
Last active May 21, 2021 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edewit/bd03bc95a169cee0ade77e0d5700a2f6 to your computer and use it in GitHub Desktop.
Save edewit/bd03bc95a169cee0ade77e0d5700a2f6 to your computer and use it in GitHub Desktop.
quick keycloak-nodejs-script

Example of how to admin keycloak

nodejs should be installed

Create a folder and node project:

mkdir keycloak-admin
cd keycloak-admin
npm init -y

Add the keycloak-admin package:

npm add keycloak-admin -D

create a script:

vim index.js

Add the following code:

#!/usr/bin/env node
const KcAdminClient = require('keycloak-admin').default;

const adminClient = new KcAdminClient({
  baseUrl: "http://localhost:8180/auth",
  realmName: 'master',
});

(async () => {
  await adminClient.auth({
    username: 'admin',
    password: 'admin',
    grantType: 'password',
    clientId: 'admin-cli',
  });

  const user = await adminClient.users.create({
      username: "test",
      email: 'test@test.com',
      enabled: true,
    });
  console.log("user created:", user);
})();

Then execute:

chmod +x index.js
./index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment