Given a synced realm with the Email/Password authentication provider enabled, is it possible via a server function to search all app users by email address ?
Hi @Mauro - welcome to the MongoDB community forum!
How I’d handle this would be to have a User
collection that contains a document for each registered user (chances are that you’ll want to store some extra data for each user anyway. You can optionally link the User
collection to the Realm user through custom data (in which case, the user token contains a snapshot of that custom data – refreshed every time the user logs in).
You can auto-populate the User
collection by setting up a Realm trigger that executes when a new user is registered:
This is the code from that trigger:
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}`);
});
};
You then have to decide how you want the mobile app to access the data from the User
collection:
- If there’s nothing sensitive in the
User
collection then you could update the Realm Sync permissions to allow any user to open up a read-only synced realm for theUser
collection - You could use sync permissions and Realm rules to lock down the
User
collection, but then add a function (that you configure to run as the system user) that performs the search for you and then returns the results to the app.
This article describe a chat app I built that uses option 1. It describes the data architecture used and includes a link to the repo containing both the iOS (SwiftUI) and Realm apps.
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.