I just started to working with Astro. I have a sidebar and the idea is to hide some items from the sidebar when the role is USER. I have the following code and this works with a static id. An atom isAdmin is set to true when the users role is ADMIN. This is used for conditional rendering the menu. How can i make this id dynamic. I tried several methods. In an astro file i can get the id from the Middleware. The id is fetched but i can not get this id to the roleStore.ts file. In roleStore.ts i tried to use Supabase getUser const { data: { user } } = await supabase.auth.getUser();
But sometimes i get the requested id but most of the times the value is undefined.
What is the best way to get a dynamic id from the logged in user? I hope someone can point me in the right direction
This is the code i use for update the const isAdmin to true or false
roleStore.ts
import { atom } from "nanostores";
import { supabase } from "@/lib/supabase";
const id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
export const isAdmin = atom(false);
async function updateAdminStatus() {
const { data, error } = await supabase
.from("profiles")
.select("role")
.eq("id", id)
.single();
if (error) {
console.error("Error fetching user role:", error);
isAdmin.set(false);
} else {
isAdmin.set(data.role === "ADMIN");
}
}
updateAdminStatus();