Resetting user passwords on Mastodon

I recently installed Mastodon (yay!), didn’t enable email sending, then promptly forgot my password (duh!). Since Mastodon is using Rails, it’s pretty straightforward to reset it.

Log into the docker container

I’m running Mastodon in Docker with docker-compose (see here for more details). If you are too, you’ll need to log in to the web container to do this. If you’re not, you can skip this.

First, find the name of the web container. It’s probably mastodon_web_1, but check with:-

$ docker ps | grep mastodon_web | awk '{ print $NF }'

…and the output should look something like this:-

mastodon_web_1

Log in to the container with the name you just got:-

$ docker exec -ti mastodon_web_1 bash

Use the rails console to reset the user’s password

Start the rails console with:-

bash-4.3$ rails c
Default type scope order, limit and offset are ignored and will be nullified
Creating scope :cache_ids. Overwriting existing method Notification.cache_ids.
Chewy console strategy is `urgent`
Loading production environment (Rails 5.2.1)
irb(main):001:0>

Get the Account object, with the username of the account whose password you want to reset. The details will also be echoed to the screen:-

irb(main):001:0> account = Account.find_by(username: 'myuser')
=> #<Account id: 1, username: "myuser", do...

Next, get the associated User object:-

irb(main):002:0> user = User.find_by(account: account)
=> User(id: integer, email: string, created_at: datetime, updated_at: datet...

Again, the details of the User object will be echoed to the screen as well as placed in the user variable. Now we can change the password attribute on the User record:-

irb(main):003:0> user.password = 'dontforgetitthistime'
=> "dontforgetitthistime"

…and then save it:-

irb(main):004:0> user.save!
[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 8975893d-ba20-3453-b5ed-2911e846276a) to Sidekiq(mailers) with arguments: "UserMailer", "password_change", "deliver_now", #<GlobalID:0x00005573acbdb148 @uri=#<URI::GID gid://mastodon/User/1>>
=> true

And you’re done! If you haven’t tried Mastodon, feel free to head over to https://mastodon.nsnw.ca/ and sign up for an account.