I’d like to delete anonymous users when they log out, since I only use them for accessing public static data for users that are not logged in to my app. Can I do that using triggers? I can’t find a “logout” trigger.
Thanks!
I’d like to delete anonymous users when they log out, since I only use them for accessing public static data for users that are not logged in to my app. Can I do that using triggers? I can’t find a “logout” trigger.
Thanks!
Currently there is no trigger based on a logout event.
I think you have 2 options :
Best
Pavel
Hi @Pavel_Duchovny,
I guess the scheduled trigger could work. How can I get all the users from a function?
Thanks,
JB
Please see the way I implemented the Realm anon-user
deletes.
Prerequistes
You will need to create 3 secrets in your application to authenticate with realm API
I’ve created the following trigger running each 5min:
And added the following function to it which runs the entire logic, remember to replace <APP-ID>
with your application id :
exports = async function() {
// Get Atlas Parameters and application id
const AtlasPrivateKey = context.values.get("AtlasPrivateKey");
const AtlasPublicKey = context.values.get("AtlasPublicKey");
const AtlasGroupId = context.values.get("AtlasGroupId");
const appId = '<APP-ID>';
// Authenticate to Realm API
const respone_cloud_auth = await context.http.post({
url : "https://realm.mongodb.com/api/admin/v3.0/auth/providers/mongodb-cloud/login",
headers : { "Content-Type" : ["application/json"],
"Accept" : ["application/json"]},
body : {"username": AtlasPublicKey, "apiKey": AtlasPrivateKey},
encodeBodyAsJSON: true
});
const cloud_auth_body = JSON.parse(respone_cloud_auth.body.text());
// Get the internal appId
const respone_realm_apps = await context.http.get({
url : `https://realm.mongodb.com/api/admin/v3.0/groups/${AtlasGroupId}/apps`,
headers : { "Content-Type" : ["application/json"],
"Accept" : ["application/json"],
"Authorization" : [`Bearer ${cloud_auth_body.access_token}`]
}
});
const realm_apps = JSON.parse(respone_realm_apps.body.text());
var internalAppId = "";
realm_apps.map(function(app){
if (app.client_app_id == appId)
{
internalAppId = app._id;
}
});
// Get all realm users
const respone_realm_users = await context.http.get({
url : `https://realm.mongodb.com/api/admin/v3.0/groups/${AtlasGroupId}/apps/${internalAppId}/users`,
headers : { "Content-Type" : ["application/json"],
"Accept" : ["application/json"],
"Authorization" : [`Bearer ${cloud_auth_body.access_token}`]
}
});
const realm_users = JSON.parse(respone_realm_users.body.text());
// Filter only anon-users
var usersToDelete = [];
realm_users.map(function(user){
if (user.identities[0].provider_type == "anon-user")
{
usersToDelete.push(user._id);
}
});
console.log(JSON.stringify(usersToDelete));
// Delete the users on the list
usersToDelete.map(function(id){
const respone_realm_users_delete = context.http.delete({
url : `https://realm.mongodb.com/api/admin/v3.0/groups/${AtlasGroupId}/apps/${internalAppId}/users/${id}`,
headers : { "Content-Type" : ["application/json"],
"Accept" : ["application/json"],
"Authorization" : [`Bearer ${cloud_auth_body.access_token}`]
}
});
});
};
This will log all the user ids that were deleted.
Please let me know if you have any additional questions.
Best regards,
Pavel
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.