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}`] } }); });
};
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");
@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.