Last active
November 22, 2021 17:39
-
-
Save luan0ap/ba894b30908fb294d26f008b356cb9ce to your computer and use it in GitHub Desktop.
Script to create several user in Keycloak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const KcAdminClient = require('@keycloak/keycloak-admin-client').default | |
const { v4: uuidv4 } = require('uuid') | |
const kcAdminClient = new KcAdminClient({ | |
baseUrl: 'http://turbina/auth', | |
realmName: 'master' | |
}) | |
await kcAdminClient.auth({ | |
grantType: 'client_credentials', | |
clientId: 'backend-cli', | |
clientSecret: 'your-secret-here' | |
}) | |
const usuarios = new Array(100).fill({ | |
name: 'Sueli Analu Isabella Moraes', | |
groups: ['/Usuarios'], | |
password: '5M92eAWB' | |
}) | |
const createUser = ({ username, password, groups, firstName, lastName }) => | |
kcAdminClient.users.create({ | |
username, | |
firstName, | |
lastName, | |
groups, | |
enabled: true, | |
emailVerified: true, | |
credentials: [{ | |
type: 'password', | |
value: password, | |
temporary: false | |
}] | |
}) | |
const getFirstLastName = (name) => { | |
const [firstName, ...lastName] = name.split(' ') | |
return { | |
firstName, | |
lastName: lastName.join(' ') | |
} | |
} | |
await Promise.all( | |
usuarios.map((usuario) => { | |
const { firstName, lastName } = getFirstLastName(usuario.name) | |
return createUser({ | |
firstName, | |
lastName, | |
username: uuidv4(), | |
password: usuario.password, | |
groups: usuario.groups | |
}).catch((e) => { | |
console.log(e) | |
return {} | |
}) | |
}) | |
) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment