Skip to content

Instantly share code, notes, and snippets.

@luan0ap
Last active November 22, 2021 17:39
Show Gist options
  • Save luan0ap/ba894b30908fb294d26f008b356cb9ce to your computer and use it in GitHub Desktop.
Save luan0ap/ba894b30908fb294d26f008b356cb9ce to your computer and use it in GitHub Desktop.
Script to create several user in Keycloak
(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