Skip to main content

Admins

Manage admin users

Create a new admin user​

This function is to create new members of the admin team.

This is done by calling the create-admin cloud function. The function takes in the following parameters: email, name and label.

The label parameter can be any of the three options: admin, manager, and support

It returns a JSON string with a error or success key that holds a message. E.g:

{
"error": "Some error message"
}
{
"success": "User created successfully"
}

Below is an example of how to call the function with the Appwrite SDK:

  try {
const res = await functions.createExecution('CREATE_ADMIN_FUNCTION_ID', JSON.stringify({
"email": "testemail@teSFSst.com",
"name": "Test Name",
"label": "support"
}));

// confirm response status code
if (res.responseStatusCode != 200) {
console.error(`Error executing function: ${res.errors}`);
// display error message to the user

}

// check the status of the function execution

if (res.status == 'processing') {
console.log('Function is still processing');
// handle processing state
return;
}

if (res.status == 'failed') {
console.error(`Function execution failed: ${res.errors}`);
// display error message to the user
return;
}

// check the output of the function execution
const body = res.responseBody;
const data = JSON.parse(body);

// check if data contains an error key
if (data.error) {
console.log(data.error);
console.error(`Function execution failed: ${data.error}`);
// display error message to the user
return;
}

// handle successful function execution
console.log('Function executed successfully:', data.success);

return;

} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error executing function: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}

List admin users​

This is done by calling the list-admins cloud function. The function takes in no parameters.

It returns a JSON string with a error or success key that holds a message. E.g.:

{
"error": "Some error message"
}
{
"success": [] // List of admin user objects
}

Below is an example of how to call the function with the Appwrite SDK:

  try {
const res = await functions.createExecution('LIST_ADMINS_FUNCTION_ID');

// confirm response status code
if (res.responseStatusCode != 200) {
console.error(`Error executing function: ${res.errors}`);
// display error message to the user

}

// check the status of the function execution

if (res.status == 'processing') {
console.log('Function is still processing');
// handle processing state
return;
}

if (res.status == 'failed') {
console.error(`Function execution failed: ${res.errors}`);
// display error message to the user
return;
}

// check the output of the function execution
const body = res.responseBody;
const data = JSON.parse(body);

// check if data contains an error key
if (data.error) {
console.log(data.error);
console.error(`Function execution failed: ${data.error}`);
// display error message to the user
return;
}

// handle successful function execution
console.log('Function executed successfully:', data.success); // holds the list of users

return;

} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error executing function: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}
info

The returned list of user is an array of Appwrite User objects.

Suspend an admin​

Instead of deleting an admin user, you can suspend them. This is done by calling the toggle-account-suspension cloud function. The function takes in the userId parameter.

It returns a JSON string with a error or success key that holds a message. E.g.:

{
"error": "Some error message"
}
{
"success": "User suspended" // or "User unsuspended"
}

Below is an example of how to call the function with the Appwrite SDK:

  try {
const res = await functions.createExecution('TOGGLE_ACCOUNT_SUSPENSION_FUNCTION_ID', JSON.stringify({
"userId": "USER_ID"
}));

// confirm response status code
if (res.responseStatusCode != 200) {
console.error(`Error executing function: ${res.errors}`);
// display error message to the user

}

// check the status of the function execution

if (res.status == 'processing') {
console.log('Function is still processing');
// handle processing state
return;
}

if (res.status == 'failed') {
console.error(`Function execution failed: ${res.errors}`);
// display error message to the user
return;
}

// check the output of the function execution
const body = res.responseBody;
const data = JSON.parse(body);

// check if data contains an error key
if (data.error) {
console.log(data.error);
console.error(`Function execution failed: ${data.error}`);
// display error message to the user
return;
}

// handle successful function execution
console.log('Function executed successfully:', data.success);

return;

} catch (error) {
if (error instanceof AppwriteException) {
console.error(`Error executing function: ${error.message} - ${error.code}`);
// display error message to the user
} else {
console.error('Unknown error:', error);
// display error message to the user
}
}