How do I save user customData on Signup

I have just been working with Realm but I have hit a dead end when trying to save user data like names and phone numbers when they sign up

so the idea is when a username signs up their full name and phone number gets saved on user data and they will receive an email for the confirmation as usual

Please help with a solution or idea on how I can go about achieving this using Realm

@Jeremie_D_Negie : Have thought of setting up a Realm function which get triggered as soon as sign up is complete.

In that function you can save all the custom information you want in the User Model.

Hi @Mohit_Sharma , thank you for your answer.

Yes, I have set up a function already but what I do not know what to do is pass data to that function.

for example, my function looks like this

exports = async function (authEvent, firstname, lastname, phoneNumber) {
  const user = authEvent.user;
  const collection = context.services
    .get("mongodb-atlas")
    .db("educators")
    .collection("user");

  const updateDoc = { firstname, lastname, phoneNumber, userID: user.id };

  const result = await collection.insertOne(updateDoc);

};

The trigger will call this function on user sign up but how do I pass the required arguments so firstname, lastname, and phoneNumber

Can someone please help with this, I have bee sitting with this problem for a week now

@Jeremie_D_Negie : Once you get the sign-up success callback on the client you can call this function to save the custom information.

@Jeremie_D_Negie did u solve the problem ? if so tell me how please

@Mohit_Sharma i am using a trigger function so how can i pass the arguments from the activity on android studio to that function ?

I wasn’t able to solve the problem ey… I ended up rebuilding the authentication with Nodejs and Postgresql so I can have more control however I was building a project with NextAuth and what I did was show a user a form on the first login to set up their profile. I guess you could set up a way to check whether a user has, for example, a username or something similar if they do you take them to whatever page you otherwise you show them a profile form where they need to fill in their information… Please let me know if you do it otherway

in the same signup activity i logged in and get the user id upload data then i logged out and moved to the login activity

Okay, but MongoDB realm sends a verify email, why not get all those data when after the user verify their email>

@Bouraoui_Ben_abdalla : You can consider checking out this repo, where I can calling a simple SUM function to add two numbers, similarly you call function and pass arguments to it.

@Jeremie_D_Negie : Did you got a chance to validate the approach I suggested you earlier ?

I did, and first I was using Realm with Nextjs, and with the Realm client in javascript you need to be authenticated to call a function so the problem I had was a new user need to verify their account first before performing an action with Realm like calling a function. so that did not work well

G’Day @Jeremie_D_Negie, @Bouraoui_Ben_abdalla :wave:,

I acknowledge it has been a while since you asked these questions. I hope you have been able to find answers to it.

For anyone else, who has the same questions.

This is user-meta data. This depends on the auth provider you are using in your application. Refer user-meta data on how to enable these fields.

Once enabled, you should be able to access these fields from your user object.

You can enable Authentication Trigger which will run the function when the user sign-up and save those details in your user collection. You can look at Task Tracker Tutorial in your preferred Tech Stack for the same functionality.

I hope the provided information helps.

Cheers, :performing_arts:

Hi… i’m having the same issue. I want to store extra information during registration(email/password provider) and reading the documentation that doesn’t seems possible? This sounds like a terrible omission IMHO.

The documentation says you have to use some pure settings like this:

const mongo = user.mongoClient("<atlas service name>");
    const collection = mongo.db("<database name>").collection("<collection name>");
    const filter = {
      userID: user.id, // Query for the user object of the logged in user
    };
    const updateDoc = {
      $set: {
        favoriteColor: "pink", // Set the logged in user's favorite color to pink
      },
    };
    const result = await collection.updateOne(filter, updateDoc);

Sorry but this is just… you don’t need to know the Atlas service name, the database name nor the collection for all your normal operations… but for this little feature your client need to know those values? It’s even worse considering that you have “refreshCustomData” right there… so the process/server knows where that data is stored. Why not add an user.updateCustomData() ?

This feature should be a priority IMHO. There are a lot of triggers during user creation that could need this information. For example registering a customer information on stripe. With customer name as a needed value.

I’m not seeing how the Authentication Trigger could be a solution considering that the information is not there yet. Unless the solution is to store the “registration” data somewhere… and then reading from there?

1 Like

I was in the same boat, I thought it was strange there would be no option to pass custom metadata on sign up. Turns out, there is! This links to the documentation section you’re looking for.

https://www.mongodb.com/docs/atlas/app-services/users/custom-metadata/#user-creation-function

the issue is that function won’t get any information you pass in a signup form. You can add custom data you already know but for example, a form with “name” using email/password credential cannot be passed. You have to do some shenanigans to get this data through

Yep, realized this myself on implementing. Spent a lot of time trying to pass custom data, but ultimately I just added a second Atlas Function that takes the custom data and adds it to the custom user data collection right after sign up.

It feels a little hacky to separate the functions, but here’s how I achieved it in my React UserContext:

1 Like

G’Day @Peter_Rauscher , and others

Many thanks for sharing your solution with the wider community :smile: . I am sure this can help people if they are having the same issue.

This is the correct solution at this time. There is no updateCustomUserData method available in the SDKs to achieve what you are looking for.

If there are any updates planned for the custom data feature, I would keep you all informed.

If there is anything else, I can help you with. Please feel free to reach out.

Cheers, :performing_arts:
henna,
Community Manager, MongoDB

1 Like

Henna,

Glad to know we’ve found the best solution for the time being. I appreciate you getting back to us and confirming the accuracy of our findings!