Realm Custom function authentication taking too long

I have implemented a custom function for user authentication in Realm and for login I have:

let authenticatedUser = await collection.findOne({ userEmail: givenEmail });
if(!authenticatedUser) 
  throw Error("User with email \"" + givenEmail + "\" does exist!");

let match = await bcrypt.compare(givenPassword, authenticatedUser.userPassword);
      
if(match)
  return { id: authenticatedUser._id.toString(), email: authenticatedUser.userEmail };
else
  throw Error("Password did not match");

The implementation works but it takes too long (1m 30s every time), which will be quite annoying to users, so I am wondering why is it taking too long. Is there something I am doing wrong?( when signing the users up, I generated the bcrypt passwords using a salt of 10 if that matters)

I decided to use Custom function auth because I didn’t know how to use the recommended email/password auth and map the authenticated user to my “Users” collection in my atlas database. I’d appreciate a guide to using the email/password auth as well

I’m attempting to use bcryptjs to hash passwords in an Atlas Function as well. I originally picked 12 rounds for the hash, but that ended with a 504 error when the function timed out.

I dropped the rounds to 1 (do not tell the cryptographers—they are an angry people) and the function to hash a user’s password now executes in about 3 seconds on a free tier cluster where I’m testing.

Did you test your function on an M10 or M20 cluster? In the past when I’ve had performance issues with Mongo, moving to at least an M10 solved them.

1 Like