Some checks failed
Deploy to Cloudflare Workers / deploy (push) Has been cancelled
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { createClient } from "@/lib/supabase/server";
|
|
import type { PortalRole } from "./roles";
|
|
import { resolvePortalRole } from "./resolve-portal-role";
|
|
|
|
export async function getCurrentProfile() {
|
|
const supabase = await createClient();
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser();
|
|
if (!user) return null;
|
|
|
|
const { role } = await resolvePortalRole(supabase, user);
|
|
|
|
const { data: profile } = await supabase
|
|
.from("profiles")
|
|
.select("id, display_name, avatar_url, portal_role")
|
|
.eq("id", user.id)
|
|
.maybeSingle();
|
|
|
|
return {
|
|
user,
|
|
profile: {
|
|
id: user.id,
|
|
display_name:
|
|
profile?.display_name ??
|
|
user.user_metadata?.display_name ??
|
|
user.email?.split("@")[0] ??
|
|
null,
|
|
avatar_url: profile?.avatar_url ?? null,
|
|
portal_role: role,
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function requirePortalRole(role: PortalRole) {
|
|
const ctx = await getCurrentProfile();
|
|
if (!ctx?.user) return { ok: false as const, reason: "auth" as const };
|
|
if (ctx.profile.portal_role !== role) {
|
|
return {
|
|
ok: false as const,
|
|
reason: "role" as const,
|
|
actual: ctx.profile.portal_role,
|
|
};
|
|
}
|
|
return { ok: true as const, ...ctx };
|
|
} |