Skip to main content

manage-admin-users

View and delete admin users

  • localhost:3000/admin-users

User data is available via Supabase client import { supabase } from "@utils/supabaseClient";

Data is fetched asynchronously and authenticated against the logged in user. A user must be an admin user in order to access the data for all admin users.=

src/pages/admin-users.js
async function getUserProfile() {
const user = supabase.auth.user();

try {
setLoading(true);

let { data, error, status } = await supabase
.from("profiles")
.select("*")
.eq("id", user.id)
.single();

if (error && status !== 406) {
throw error;
}

if (data) {
setUserProfile(data && data);
}
} catch (error) {
// console.log("user not logged in");
} finally {
setLoading(false);
}
}

async function getProfiles() {
const user = supabase.auth.user();

try {
setLoading(true);

let { data, error, status } = await supabase
.from("profiles")
.select("*")
.eq("organisation", organisation)
.eq("admin_user", true);

if (error && status !== 406) {
throw error;
}

if (data) {
setUserData(data);
}
} catch (error) {
// console.log("user not logged in");
} finally {
setLoading(false);
}
}

Users can also be deleted. This requires both the deletion of the user's profile in the database, but also the user from the auth table in Supabase. This is done using a serverless function. In addition the user's ID must be removed from the admin table in Supabase

src/pages/admin-users.js
async function deleteUserProfile(id) {
try {
setLoading(true);

let { error } = await supabase.from("profiles").delete().match({ id: id });

if (error) {
throw error;
}
} catch (error) {
alert(error.message);
} finally {
deleteUserAdmin(id);
setLoading(false);
}
}

// Delete ID from admins table
async function deleteUserAdmin(id) {
const userDataToSend = {
entry: id,
};

try {
setLoading(true);

let { error } = await supabase
.from("admins")
.delete()
.match({ user_id: id });

if (error) {
throw error;
}
} catch (error) {
alert(error.message);
} finally {
try {
setLoading(true);

const { data, error } = await supabase.functions.invoke("delete-user", {
body: JSON.stringify(userDataToSend),
});

if (error) alert(error);

if (data) {
if (data.error) {
console.log(data.error);
}
}
} catch (error) {
console.log(error);
} finally {
getProfiles(); // reload the data to remove from table
setLoading(false);
}
}
}