Skip to content

Instantly share code, notes, and snippets.

@david-uhlig
Last active April 23, 2024 13:27
Show Gist options
  • Save david-uhlig/7a94bc831477f846d312a9f9b01afb00 to your computer and use it in GitHub Desktop.
Save david-uhlig/7a94bc831477f846d312a9f9b01afb00 to your computer and use it in GitHub Desktop.
Rocket Chat: Convert User Emails to Lowercase

Issue

Rocket Chat's password reset feature fails if the user has uppercase characters in their email address, e.g. John.Doe@example.com. An issue has been raised. Until it is resolved, a possible workaround is to convert all your users' email addresses to lowercase.

Workaround

This solution converts all upper case email addresses to lower case. It assumes that Rocket Chat is run through docker compose, but can easily be applied to any other setup with minor changes.

First, create a database dump in case something goes wrong

docker compose exec mongodb sh -c 'mongodump --archive' > db.dump

Then, load the interactive JavaScript shell to MongoDB inside the container

docker compose exec -it mongodb mongo

and select the rocketchat database

use rocketchat

Check to see how many records are affected

db.users.find({ "emails.address": { $regex: /[A-Z]/ }}).count()

Update all instances to lowercase

db.users.find({ "emails.address": { $regex: /[A-Z]/ }}).forEach(function(doc) { 
  doc.emails.forEach(function(email) {   
    email.address = email.address.toLowerCase();
  }); 
  db.users.update({ "_id": doc._id }, doc); 
});

Check again how many records are affected, the query should have a return value of 0 this time

db.users.find({ "emails.address": { $regex: /[A-Z]/ }}).count()

Tested on

  • Rocket Chat 6.7.0
  • MongoDB 5.0.24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment