AUTHENTICATE Trigger Snippet Signup Example

Hi, I am new to Realm and I was looking for an example of how to use the AUTHENTICATE trigger snippet for signup. I do not know how to pass the variables from my android application to the trigger snippet. The collection I want to pass the details to, upon signup, is UserDetails having fields:
{ UserID, Email, Phone, Gender, DOB} and my variables in my android app name are: email, phoneNum, gender, dateOfBirth.

I have tried the following code and it triggers after the new email logins but obviously, only the email is being passed correctly since I do not know how to pass the other variables. I can’t find any examples either on the web.

//Trigger function
exports = function(authEvent){
  // Only run if this event is for a newly created user.
  if (authEvent.operationType !== "CREATE") { return }

  // Get the internal `user` document
  const { user } = authEvent;

  const users = context.services.get("mongodb-atlas")
    .db("RecommenderAppDB")
    .collection("UserDetails");

  const isLinkedUser = user.identities.length > 1;

  if(isLinkedUser) {
    const { identities } = user;
    return users.updateOne(
      { id: user.id },
      { $set: { identities } }
    )

  } else {
    return users.insertOne({ _id: user.id,
    UserID: user.id,
    Email: user.data.email,
      Phone: user.data.phoneNum,
      Gender: user.data.gender,
      DOB: user.data.dateOfBirth
    })
     .catch(console.error)
  }
};

How do I pass the variables from my android app using the registerUserAsync() method?

Any ideas will b greatly appreciated!

Hi @Praveer_Ramsorrun, welcome to the community forum!

You can’t include the extra information a part of the call to register the user.

You can create a user object after the registration, and that object will get synced to MongoDB (you can then optionally have a trigger that processes that data).

Hi, thank you for replying. Yes, I understand that extra info cannot be passed with the register method. But, how do I pass data from my app which can then be used in the trigger functions? For example if I want to pass a user’s name with variable uName, to the trigger function. How do I proceed? Apologies if these queries seem trivial, but I cannot find info about these elsewhere.

Hi @Praveer_Ramsorrun,

how I tend to handle it is to store that data in a collection. I have an authentication trigger that runs when a user registers. Here’s an example…

exports = function({user}) {
  const db = context.services.get("mongodb-atlas").db("RChat");
  const userCollection = db.collection("User");
  
  const partition = `user=${user.id}`;
  const defaultLocation = context.values.get("defaultLocation");
  const userPreferences = {
    displayName: ""
  };
  
  console.log(`user: ${JSON.stringify(user)}`);
  
  const userDoc = {
    _id: user.id,
    partition: partition,
    userName: user.data.email,
    userPreferences: userPreferences,
    location: context.values.get("defaultLocation"),
    lastSeenAt: null,
    presence:"Off-Line",
    conversations: []
  };
  
  return userCollection.insertOne(userDoc)
  .then(result => {
    console.log(`Added User document with _id: ${result.insertedId}`);
  }, error => {
    console.log(`Failed to insert User document: ${error}`);
  });
}

In general, you can run functions as the current user – and so you can access the user id from the context using context.user.id. You can then use that to fetch data from your User collection.

Triggers execute functions as the system user and so this information isn’t available in context.user and so you need to include something within the modified document that can be used to identify the user that updated the collection.

1 Like