Create a new user with custom_data from the client (Web sdk)

I’m trying to run functions insertOne, updateOne from web client (so can pass registration variables like nickname and comments), not via a registerUser (create) realm server trigger with this code:

await realmapp.emailPasswordAuth.registerUser({ email, password });

//Immediately login the new user
//(to go past 'pending' in realm console)
const newUsercredentials = Realm.Credentials.emailPassword(email, password);
const loggedInNewUser = await realmapp.logIn(newUsercredentials);

console.log('new
user just registered : ', loggedInNewUser);

const newUser = await users.insertOne({ id:
loggedInNewUser.id });

await users.updateOne({ id:
loggedInNewUser.id },

{ $set:
{
"custom_data.active": true,
"custom_data.description":{level: '',
comment: ''
},
"custom_data.alternate_email": "",
"custom_data.nickname": "",
"custom_data.ownerOf": [],
"custom_data.memberOf": []
}
});

In http headers:
Request id for the newly created user is correct:

63366590c70e5740bff597fa

but I get Response:

error "invalid session: No user found for session for user id '633664ccc59dec10f25094ef '

or

error	"invalid session: access token expired"

error_code "InvalidSession"

i.e. response indicates wrong user id or access/refresh token expired

Why is this happening? What can I do to ensure a valid user session and complete the creation of the necessary user document and associated custom_data without using server side triggers that depend on server side functions? thanks …

SOLUTION:
I had to manually assign the refresh token to the session storage with the logged in user’s id:
sessionStorage.setItem('realm-web:app(<my_app_id>):user(' + loggedInNewUser.id + '):refreshToken:', loggedInNewUser.refreshToken);
This is not mentioned in the docs as far as I could see. If there is a better way, please let me know here.

1 Like

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