App.EmailPassword.register()

I have one scenario when admin needs to create other users account. where he provide email, password and other custom info too. now when Admin add new user, I call app.emailPasswordAuth.registerUser(). to get register with application. It will take only email and password. I want to store custom info based of Id return by app.emailPasswordAuth.registerUser(). but unfortunately it won’t return any id for newly created user. how facing a problem to map user info into mongo atlas db.
here is my code:

const newUser = await app.emailPasswordAuth.registerUser('abc@gmail.com', '123456');
const config = {
        sync: {
        user: user,
        partitionValue: 'PUBLIC',
}
Realm.open(config).then((userRealm) => {
     userRealm.write(() => {
      const result = userRealm.create(
          "User",
           new User ({
           _id: ( I want to put id of newly register user here),
           _partition: `User=${Id}`,
            name: email,
           role:role
          })
        );
      
      }); 
});

Can you please guide me how it will be done. Thank you in advance!!!

The user id gets populated the first time the user logs in with the credentials. So you probably need to add something like:

const user = await app.login(Credentials.emailPassword('abc@gmail.com', '123456'));

// ...

_id: user.id

But I want to define roles before he/she logs in so that I can assign login flow based on role. N that’s why I need ID to add custom information to user before he/she logs in.

I think my question is not clear to you. let me try again.
For example you are Admin who is currently logged into the system. now he add users( employees). so he called app.emailPasswordAuth.registerUse() to register employe with App. Admin also want to store Employees Address,Phone num etc. It is only possible if I get the Id of Employee registered with the app.
As u say app.login() will populate the Id of employee…but then he will get logged in when Admin is already logged in into the app.

I understand the question, but am not sure what your concern is with logging in the user. There are no issues with logging in multiple users on the same device and you don’t have to transition the app UI to indicate a new user has logged in. Here’s an example function that hopefully clarifies my point:

async function registerNewUser(email, password) {
    // Store the current user to switch back to it
    const currentUser = app.currentUser;

    // Login new user to allocate an Id for them
    await app.emailPasswordAuth.registerUser(email, password);
    const newUser = await app.login(Credentials.emailPassword(email, password));

    // We have the user id now, let's log them out and remove them from the local device
    await app.removeUser(newUser);

    // Switch back to the original currentUser
    app.switchUser(currentUser);

    // Return the user Id to setup Address, Phone, and so on.
    return newUser.id;
}

In your code, instead of calling app.emailPasswordAuth.registerUser, you can now call the new function and get the Id of the newly registered user.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.