Hi everyone, I apologize in advance if my question may be trivial, but I am now learning how to use MongoDB, and some parts of the documentation are difficult for me to understand.
In my application I don’t have the possibility to implement the Realm SDK, so I was thinking to manage the user/database communication via Webhooks.
First I thought to create a Custom Function authentication, in order to authenticate a user. I was trying to create a simple authentication function like this one:
exports = async function(loginPayload) {
// Get a handle for the app.users collection
const users = context.services
.get("mongodb-atlas")
.db("app")
.collection("users");
// Parse out custom data from the FunctionCredential
const { username } = loginPayload;
// Query for an existing user document with the specified username
const user = await users.findOne({ username });
if (user) {
// If the user document exists, return its unique ID
return user._id.toString();
} else {
// If the user document does not exist, create it and then return its unique ID
const result = await users.insertOne({ username });
return result.insertedId.toString();
}
};
And then recall it via a Webhook.
Is this correct as a reasoning? Or am I adopting the wrong method?
If everything is correct I will proceed by explaining my current problem. I tried to create a new HTTP Webhook by selecting the “POST” method (since I have to communicate to the function the username of the player to be searched in the database), but I have some difficulties to create the url and the Webhook function.
How should the parameters of the url indicated by the Webhook be set? How can I read this data inside the function and then recall the custom authentication function?
Searching inside the documentation I think I have understood that I have to recall the custom function in this way:
const loginResult = context.functions.execute(“authFunc”, arg1);
return loginResult;
But unfortunately I can’t test this code since, as I wrote earlier, I can’t understand how to correctly read the data received by the “POST” method and assign the user’s username to the variable “arg1”.
Can someone give me some indication about this?
Thanks in advance!