forked from fahrPool/fahrPool
38 lines
834 B
TypeScript
38 lines
834 B
TypeScript
import { cache } from "react";
|
|
import { createClient } from "@/lib/supabase/server";
|
|
import type { Role } from "./roles";
|
|
|
|
export type Profile = {
|
|
id: string;
|
|
name: string;
|
|
role: Role;
|
|
};
|
|
|
|
export type ProfileResult =
|
|
| { status: "unauthenticated" }
|
|
| { status: "missing-profile" }
|
|
| { status: "ok"; profile: Profile };
|
|
|
|
export const loadProfile = cache(async (): Promise<ProfileResult> => {
|
|
const supabase = await createClient();
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser();
|
|
|
|
if (!user) {
|
|
return { status: "unauthenticated" };
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from("profiles")
|
|
.select("id, name, role")
|
|
.eq("id", user.id)
|
|
.single();
|
|
|
|
if (error || !data) {
|
|
return { status: "missing-profile" };
|
|
}
|
|
|
|
return { status: "ok", profile: data as Profile };
|
|
});
|