Answers.Body: <p>You can't use the <code>users.onBeforeSaveUser</code> event to set a user's password as this event happens after the supplied password has been hashed by a private method in the <code>UsersService</code>.</p>
  
  <p>Instead, you could use the <code>users.onSaveUser</code> event, check if a new user has just been saved and if so add the password to the <code>userModel</code> and re-save it. The following is untested, but should do the trick:</p>
  
  <pre><code>    craft()-&gt;on('users.saveUser', function(Event $event) {
  
          // Only fire if new user, this should avoid an infinite loop
          if ($event-&gt;params['isNewUser']) {
  
              // retrieve the userModel from the event
              $user = $event-&gt;params['user'];
  
              // set new password
              $user-&gt;newPassword = 'My Super Secret Password';
  
              // save user
              if (craft()-&gt;users-&gt;saveUser($user))
              {
                  // Password successfully saved!
              }
              else
              {
                  // Oops, something went wrong!
              }
          }
      });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-14T14:25:59.897Z
Answers.Text: thanks @SteveRowling - that seems to have done the trick. Seem to be going round in circles on this plugin but seem to be nearly there … :D
Answers.UserDisplayName: Cole Henley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-14T13:39:01.293Z
Answers.LastActivityDate: 2016-06-14T13:39:01.293Z
Answers.UserDisplayName: Steve Rowling
Body: <p>I'm trying to update a user password on save via a custom field (on registration via the back-end - the admin needs to specify a password on registering a user). Is this even possible?</p>
  
  <p>Came across this question <a href="https://craftcms.stackexchange.com/questions/1197/how-do-i-change-a-users-password-programmatically-in-a-plugin">How do I change a user&#39;s password programmatically in a plugin?</a> and tried using the <code>users.onBeforeSaveUser</code> event to update the <code>$user-&gt;newPassword</code> but the user cannot login with the details registered/created.</p>
  
  <p>Any thoughts on above appreciated!</p>
  
Tags: <plugin-development><users><registration><password>
Title: Update user password
ThreadText: # Update user password
  
  (Asked by: username_0 on 2016-06-14)
  
  I'm trying to update a user password on save via a custom field (on registration via the back-end - the admin needs to specify a password on registering a user). Is this even possible?
  
  Came across this question [How do I change a user's password programmatically in a plugin?](https://craftcms.stackexchange.com/questions/1197/how-do-i-change-a-users-password-programmatically-in-a-plugin) and tried using the `users.onBeforeSaveUser` event to update the `$user->newPassword` but the user cannot login with the details registered/created.
  
  Any thoughts on above appreciated!
  
  **Tags:** plugin-development, users, registration, password
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-06-14. Score: 2 | Accepted answer
  
  You can't use the `users.onBeforeSaveUser` event to set a user's password as this event happens after the supplied password has been hashed by a private method in the `UsersService`.
  
  Instead, you could use the `users.onSaveUser` event, check if a new user has just been saved and if so add the password to the `userModel` and re-save it. The following is untested, but should do the trick:
  
  ```
      craft()->on('users.saveUser', function(Event $event) {
  
          // Only fire if new user, this should avoid an infinite loop
          if ($event->params['isNewUser']) {
  
              // retrieve the userModel from the event
              $user = $event->params['user'];
  
              // set new password
              $user->newPassword = 'My Super Secret Password';
  
              // save user
              if (craft()->users->saveUser($user))
              {
                  // Password successfully saved!
              }
              else
              {
                  // Oops, something went wrong!
              }
          }
      });
  
  ```
  
  > **Comments:**
  > **username_0**: thanks @username_1 - that seems to have done the trick. Seem to be going round in circles on this plugin but seem to be nearly there … :D  
  
  ---
  
  