1
0
Fork 0
fahrPool/web/lib/auth/profile.ts

39 lines
871 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;
firma_id: string | null;
};
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, firma_id")
.eq("id", user.id)
.single();
if (error || !data) {
return { status: "missing-profile" };
}
return { status: "ok", profile: data as Profile };
});