Hard code the Strapi administrator

Here a snippet to programatically create a super admin user in Strapi v4:

const hasAdmin = await strapi.service('admin::user').exists();

if (hasAdmin) {
  return;
}

const superAdminRole = await strapi.service('admin::role').getSuperAdmin();

if (!superAdminRole) {
  return;
}

await strapi.service('admin::user').create({
  email: 'test@test.com',
  firstname: 'first name',
  lastname: 'last name',
  password: 'password',
  registrationToken: null,
  isActive: true,
  roles: superAdminRole ? [superAdminRole.id] : [],
});
1 Like