Hello @Jesus_Santin
The way a custom function authentication works could be explained as follows:
I have a custom example payload defined as:
const payload = {
uid: 100,
user: "custom_function",
email: "cf@josman.com",
name: "custom function",
provider_type: "custom_function"
}
This is the payload I am going to use for this example user custom_function
whenever it needs to authenticate against my Realm Application. I have my own business logic in where every user will have an internal uid
that will keep track of every user independently.
When I run the following code in my application:
const credentials = Realm.Credentials.function(payload);
async function authenticate() {
console.log("AUTHENTICATE");
try {
const user = await app.logIn(credentials);
return user.id;
} catch (err) {
console.error(err);
}
}
What I am doing is authenticating this user against my Realm backend. At this moment, there are two possible scenarios: either the user exists and I am performing a login or the user did not exist in my Realm app and I am registering it for the first time.
My Realm function linked to the Custom Authentication is as follows
exports = async function (loginPayload) {
console.log(JSON.stringify(loginPayload));
const dbName = context.values.get('db_name');
const users = context.services.get('mongodb-atlas').db(dbName).collection("Users");
// Parse out custom data from the FunctionCredential
const { uid } = loginPayload;
// Query for an existing user document with the specified uid
const user = await users.findOne({ uid });
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(loginPayload);
return result.insertedId.toString();
}
};
This will either create the user or if exist letting them access the app.
I know that your specific example is not exactly the same use case, as you will validate the id
as a sort of apiKey
but the principle is the same.
In @Pavel example, he is sending the payload in the body of the HTTP request, as you can see in the cURL:
curl \
-H "Content-Type: application/json" \
-d '{"user_id":"5fa7105a871d206bd6739a4", "comment_id" : 1, "post_id" : 1, comment : "great post!" }' \
https://webhooks.mongodb-realm.com/api/client/v2.0/app/app-abcd/service/myTest/incoming_webhook/storeComment
More precisely: -d '{"user_id":"5fa7105a871d206bd6739a4", "comment_id" : 1, "post_id" : 1, comment : "great post!" }'
In both examples, you need to create the payload that you will later on access in your function. If you are getting undefined
, it could be that there is a difference between what you are sending and the key you are accessing.
Could you try printing the payload? Add the following to your function:
console.log(payload);
And look in the logs for the information that is stored here to check whether you are accessing the correct key or not.
I hope you found my explanation useful. Please do not hesitate to contact me if you need any further clarification or if you would like to provide us with further details.
Kind Regards,
Josman