Skip to content

Instantly share code, notes, and snippets.

@juandm
Last active April 4, 2021 17:51
Show Gist options
  • Save juandm/db335e5967d9431f45de5256d7919ebb to your computer and use it in GitHub Desktop.
Save juandm/db335e5967d9431f45de5256d7919ebb to your computer and use it in GitHub Desktop.
Recover strapi admin password in development mode

Strapi recover admin password in development mode

  1. Open a console inside the project folder.

  2. execute npx strapi console command

  3. Copy & paste this code snippet in the console

     function changePassword(useremail, password) {
       strapi.admin.services.user.findOne({ email: useremail })
       .then((u) => {
         if (u) {
           strapi.admin.services.auth
             .hashPassword(password)
             .then((hashedPassword) => {
               strapi
                 .query("user", "admin")
                 .update({ id: u.id }, { password: hashedPassword })
                 .then(() => console.log("Updated successfully."))
                 .catch((ex) => console.error("Failed to update password.", ex));
             })
             .catch((ex) =>
               console.error("Failed to hash password and update it.", ex)
             );
         } else {
           console.error("Wrong email?? Please check your email");
         }
       });
     }
     changePassword("your_admin_user", "your_admin_password");

    One line version function changePassword(useremail, password) { strapi.admin.services.user.findOne({ email: useremail }).then(u => { if (u) { strapi.admin.services.auth.hashPassword(password).then(hashedPassword => { strapi.query('user', 'admin').update({id: u.id }, { password: hashedPassword }).then(() => console.log('Updated successfully.')).catch((ex) => console.error('Failed to update password.', ex)) }).catch(ex => console.error('Failed to hash password and update it.', ex)); } else { console.error('Wrong email?? Please check your email'); } }); } changePassword('your_admin_user', 'your_admin_password');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment