mongodbauthenticationrealmbackendmongodb-realm

Linking Google SignIn and Email/Password login in Mongo Realm


We have used Mongo Realm for our React Native project. I have noticed that while creating an account using the simple create account method and Google sign, even tho we use the same email, two different accounts are created, is there any way to link both these created accounts?

I checked the mongo realm documentation, but it wasn't that big of a help.

This is how Mongo Team responded to this issue

So, is there any other way to link the same user with different authentication providers?


Solution

  • So, what I did was

    const res = await GoogleSignIn();
    const ID = res.idToken;
    const credential = Realm.Credentials.google({ idToken: ID });
    const userID = await realmAppID.logIn(credential);
    

    Here, after completing the google sign in we will be getting a new user ID, and user's email.

    await user.updateOne(
            { email: res.user.email },
            {
              $set: {
                name: res.user.name,
                email: res.user.email,
                profileImage: res.user.photo,
              },
              $addToSet: { userId: Realm.BSON.ObjectID(userID.id) },
            },
            { upsert: true },
          );
    

    Here, we would check if an account with a similar email id exists or not, if YES, then we would add the newly obtained userId to the existing user's ID.

    We can follow the same method after the user login using email/ password.

    We would change the field userId as an array in the database