Delete anonymous users upon log out: trigger?

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!

Hi @Jean-Baptiste_Beau,

Currently there is no trigger based on a logout event.

I think you have 2 options :

  1. Have a scheduled trigger to periodically query users that are logout and delete them.
  2. On logout client
    logic insert a document to a collection and build a special database trigger to delete this user id.

Best
Pavel

Hi @Pavel_Duchovny,

I guess the scheduled trigger could work. How can I get all the users from a function?

Thanks,
JB

Hi @Jean-Baptiste_Beau,

I will sketch something for you in the upcoming days…

Best
Pavel

Hi @Jean-Baptiste_Beau,

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

  • AtlasPrivateKey - Holds an admin of the Atlas private key
  • AtlasPublicKey - Holds an admin of the Atlas public key
  • AtlasGroupId - Holds the relevant project Id

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

6 Likes

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