# Reset password with custom ruby file

**URL:** https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184
**Category:** Development
**Created:** [September 26, 2023, 3:16pm UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184 "2023-09-26T15:16:32Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Hitesh\_Sharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hitesh_sharma/32/309992_2.png) [@Hitesh\_Sharma](https://meta.discourse.org/u/Hitesh_Sharma)
#### Post date: [September 26, 2023, 3:16pm UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/1 "2023-09-26T15:16:33Z")

</div>

Custom function for password update in discourse

Can we use this code to update the password?

e.g:-  
User.find\_by(username: ‘gollum’).update!(password: ‘shiiiire!-BAGGINS!’)

Or

Can we create a custom plugin and use a custom endpoint like API  
and create a token with user credentials then reset the password with API.

> **[Discourse API Docs](https://docs.discourse.org/#tag/Users/operation/changePassword)**

Please share best way to do this task

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 26, 2023, 3:20pm UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/2 "2023-09-26T15:20:43Z")

</div>

Hi Hitesh,

What would be your use case? Why not use the built-in feature?

---

<div class="post-metadata">

### Author: ![Hitesh\_Sharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hitesh_sharma/32/309992_2.png) [@Hitesh\_Sharma](https://meta.discourse.org/u/Hitesh_Sharma)
#### Post date: [September 26, 2023, 3:31pm UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/3 "2023-09-26T15:31:38Z")

</div>

thanks for reply

I am using a custom script to migrate Drupal users to discourse but the password is saved as a hash string and the user can not log in with a reliable password like (admin@123)

But It login with their password hash like  
e.g.:- 3sdfd32423@#423fdsfr

So can you please little explain regarding built-in feature

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 26, 2023, 3:51pm UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/4 "2023-09-26T15:51:30Z")

</div>

Have you had a look at this? It might help:

> [@Migrated password hashes support](https://meta.discourse.org/t/migrated-password-hashes-support/19512):
>
> Here is the password migration support plugin: [https://github.com/communiteq/discourse-migratepassword/](https://github.com/communiteq/discourse-migratepassword/)The original thread follows. We do regular forum conversions (yes, we’ll open source the converter once it’s stable enough) and one of the big quirks when migrating to Discourse is the fact that all users have to set a new password, because the password in the original forum is encrypted. So we thought of the following mechanism. when creating users in the conversion step, we store the …

It supports Drupal 7 passwords:

> <https://github.com/communiteq/discourse-migratepassword/blob/master/plugin.rb#L20>

---

<div class="post-metadata">

### Author: ![Hitesh\_Sharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hitesh_sharma/32/309992_2.png) [@Hitesh\_Sharma](https://meta.discourse.org/u/Hitesh_Sharma)
#### Post date: [September 26, 2023, 4:01pm UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/5 "2023-09-26T16:01:40Z")

</div>

hi @Canapin

Also, I used this plugin but it is not working

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 26, 2023, 10:10pm UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/6 "2023-09-26T22:10:05Z")

</div>

> [@Hitesh\_Sharma](#):
>
> but it is not working

No - it’s working.

This is one of the oldest third party plugins around (over 9 years old now!) and it has been maintained and working all the time. It might not be working _for you_ but the way to solve that is to find why it is not working for you instead of trying an alternative route.

> [@Hitesh\_Sharma](#):
>
> But It login with their password hash like  
> e.g.:- 3sdfd32423@#423fdsfr

Can you please rephrase this? I don’t understand what you are saying.

---

<div class="post-metadata">

### Author: ![Hitesh\_Sharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hitesh_sharma/32/309992_2.png) [@Hitesh\_Sharma](https://meta.discourse.org/u/Hitesh_Sharma)
#### Post date: [September 27, 2023, 6:35am UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/7 "2023-09-27T06:35:50Z")

</div>

> [@Hitesh\_Sharma](#):
>
> But It login with their password hash like  
> e.g.:- 3sdfd32423@#423fdsfr

Is my Drupal website storing user passwords in an encrypted code and keeping them in a database I moved all the users to a platform called Discourse using a dsiscourse API. I create a payload with their user details and password codes from Drupal’s database and put them into Discourse.

I thought users would be able to use their same login details on Drupal, but it’s not working. Discourse doesn’t accept the user’s actual password, like “admin@123.” Instead, it uses a user password hash code like “3sdfd32423@#423fdsfr.”

Discourse takes the password codes from Drupal and puts them into its own encrypted algo in its own database.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 27, 2023, 6:46am UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/8 "2023-09-27T06:46:34Z")

</div>

> [@Hitesh\_Sharma](#):
>
> I create a payload with their user details and password codes from Drupal’s database and put them into Discourse.

Can you show us how you did it?  
If you have been using the API, you should not set the Drupal hash as the password. You should set the hash as a custom user field called `import_pass`. I’m not sure if that is even possible using the API.

---

<div class="post-metadata">

### Author: ![Hitesh\_Sharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hitesh_sharma/32/309992_2.png) [@Hitesh\_Sharma](https://meta.discourse.org/u/Hitesh_Sharma)
#### Post date: [September 27, 2023, 6:56am UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/9 "2023-09-27T06:56:05Z")

</div>

I followed your steps while installing the Discourse Migrate plugin

→ First I installed the plugin into my discourse but did not enable then created a  
the custom field into discourse with the name import\_pass

→ In the payload add the password hash stored in this custom field

→ After the user uploads into the discourse enable the plugin and log in new window with user credentials but they didn’t

 ![Screenshot from 2023-09-27 12-18-33](https://global.discourse-cdn.com/meta/original/4X/2/3/0/2308d5eb0bd448dc9fc5be12ed8823f3f428267e.png)

 ![Screenshot from 2023-09-21 18-38-12](https://global.discourse-cdn.com/meta/original/4X/6/a/f/6af90139fbd945652216c1a87920f62337183ca8.png)

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 27, 2023, 7:06am UTC](https://meta.discourse.org/t/reset-password-with-custom-ruby-file/280184/10 "2023-09-27T07:06:28Z")

</div>

> [@Hitesh\_Sharma](#):
>
> then created a  
> the custom field into discourse with the name import\_pass

This is [explicitly](https://meta.discourse.org/t/drupal-password-migrate-into-discourse-with-discourse-migrate-password-plugin/279694/2) _not_ part of the steps.

There is a difference between creating a user field with that name, and having a custom field with that name. So the hash is being stored in the wrong place.

The problem is that I think that the latter (a custom field) is not accessible using the API.  
I think the best way forward is to have some custom once-off code that moves the content of the user field to the actual custom field.

(I considered changing the plugin so it looks at the user field as well but I think surfacing those hashes to the user interface is a security risk, so not going to do that).

This will work, run it from a rails console (tagging you so you see the edit @Hitesh_Sharma )

```rb
ufkey = "user_field_#{UserField.find_by(name: 'import_pass').id}"
User.all.each do |u|
  if u.custom_fields.key?(ufkey)
    u.custom_fields[:import_pass] = u.custom_fields[ufkey]
    u.custom_fields.delete(ufkey)
    u.save_custom_fields
  end
end

```
