Hard code the Strapi administrator

This code will create the strapi-super-admin role as it doesn’t exist on the first run, then it will create the super-admin user. For security reasons I’ve added it only for “development” environment, to avoid cases when developers forget about this code in production.

module.exports = async () => {
  if (process.env.NODE_ENV === 'development') {
    const params = {
      username: process.env.DEV_USER || 'admin',
      password: process.env.DEV_PASS || 'admin',
      firstname: process.env.DEV_USER || 'Admin',
      lastname: process.env.DEV_USER || 'Admin',
      email: process.env.DEV_EMAIL || 'admin@test.test',
      blocked: false,
      isActive: true,
    };
    //Check if any account exists.
    const admins = await strapi.query('user', 'admin').find();

    if (admins.length === 0) {
     try {
        let tempPass = params.password;
        let verifyRole = await strapi.query('role', 'admin').findOne({ code: 'strapi-super-admin' });
        if (!verifyRole) {
        verifyRole = await strapi.query('role', 'admin').create({
          name: 'Super Admin',
          code: 'strapi-super-admin',
          description: 'Super Admins can access and manage all features and settings.',
         });
        }
        params.roles = [verifyRole.id];
        params.password = await strapi.admin.services.auth.hashPassword(params.password);
        await strapi.query('user', 'admin').create({
          ...params,
        });
        strapi.log.info('Admin account was successfully created.');
        strapi.log.info(`Email: ${params.email}`);
        strapi.log.info(`Password: ${tempPass}`);
      } catch (error) {
        strapi.log.error(`Couldn't create Admin account during bootstrap: `, error);
      }
    }
  }
};

5 Likes