Skip to main content

manage-users

Edit and delete users

  • localhost:3000/

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

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

src/pages/index.js
async function getProfiles() {
try {
setLoading(true);
const user = supabase.auth.user();

let { data, error, status } = await supabase
.from("profiles")
.select("*")
.neq("admin_user", true)
.neq("id", user.id)
.eq("organisation", organisation)
.order("updated_at", { ascending: false });

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.

src/pages/index.js
async function deleteUserProfile(id) {
const userDataToSend = {
entry: id,
};
try {
setLoading(true);

let { error } = await supabase.from("profiles").delete().match({ 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);
}
}
}