Remove Authenticated users

I would like for my ios and android clients to be able to delete their account through the mobile app, meaning delete all their data and also delete the authorized user. Deleting all their data is easily done, but how do I programmatically delete the user? Can it be done through a function which can be called from the app if the user id is passed to the function?

Hi @Deji_Apps ,

I suggested a function approach on deleting anonymous users with api

I believe the same approach can be done here

Thanks
Pavel

@Pavel_Duchovny
You are right about your approach. I tried modifying your anonymous user delete to delete a specific user that matches a specific user id. However I keep getting this error:

{“message”:“‘map’ is not a function”,“name”:“TypeError”}
{
“arguments”: [
“60d4e453482d128a8c0ac15c”
],
“name”: “deleteUser”
}

Here is the what I tried:

exports = async function(userId) {

// 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 = ‘my_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)
{
console.log(JSON.stringify(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._id == userId)
 {
   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}`]
}

 });
 });

};

1 Like

Hi @Deji_Apps ,

Please add some prints before and after every map operation so we can narrrow down the function issue.

Best regards,
Pavel

For those coming here and who have the same problem as @Deji_Apps and me: {“message”:“‘map’ is not a function”,“name”:“TypeError”}

Check here:

You cannot directly read the value of a Secret after defining it. Instead, you link to the Secret by name in authentication provider and service configurations. If you need to access the Secret from a Function or Rule, you can link the Secret to a Value.

You will end up with something like this:

Then in your code:

  const AtlasPrivateKey = context.values.get("PrivateKey");
  const AtlasPublicKey = context.values.get("PublicKey");
  const AtlasGroupId = context.values.get("GroupId");
1 Like

@Deji_Apps in addition to @Mike_Notta last reply, you should create API key with Owner Access (Admin Access is not enough) to delete user from Realm App. Also, don’t forget to make corresponding changes to your Secrets in Values before executing the function.

1 Like