Hi there, I’m newbie in Strapi and I’d like to use the bootstrap function to:
create an admin user if it doesn’t exist
create some sample data
I’ve been reading the documentation, but failed to find what’s expected. I’ve been searching in forums, but it seems to have big differences between versions.
This is the snippet I’m trying to run (unsuccessfully):
However, this is not working properly, and even the logs complain:
Admin UI built successfully
[2022-07-19 11:33:13.148] warn: Your application doesn't have a super admin user.
[2022-07-19 11:33:13.205] warn: Some users (1) don't have any role.
Any hint on how to properly relate the user with its role so after this bootstrap we can say there is a super-admin user?
Thanks for your post. I took your suggested code and applied a couple of tweaks and it worked for me on v4.4.1:
// src/index.ts
export default {
/**
* An asynchronous register function that runs before
* your application is initialized.
*
* This gives you an opportunity to extend code.
*/
register(/*{ strapi }*/) {},
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
*
* This gives you an opportunity to set up your data model,
* run jobs, or perform some special logic.
*/
async bootstrap({ strapi }) {
// create user admin if it doesn't exist
await strapi.admin.services.role.createRolesIfNoneExist();
const superAdminRole = await strapi.db.query('admin::role').findOne({where: {code: 'strapi-super-admin'}});
const superAdmin = await strapi.db.query('admin::user').findOne({where: {username: 'admin'}});
if (!superAdmin) {
const params = {
username: 'admin',
email: 'admin@email.com',
blocked: false,
isActive: true,
confirmed: true,
password: null,
roles: null
}
params.password = await strapi.admin.services.auth.hashPassword("Admin1234");
params.roles = [superAdminRole.id]
await strapi.db.query("admin::user").create({
data: {...params},
populate: ['roles']
});
}
}
};