feat: add better auth sqlite runtime and server-side session flow
This commit is contained in:
+14
-53
@@ -1,71 +1,32 @@
|
||||
import { PortalShell } from "@/components/layout/portal-shell";
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { redirect } from "next/navigation";
|
||||
import { requireClientUser } from "@/server/auth/session";
|
||||
|
||||
export default async function PortalLayout({
|
||||
children,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
const { user, profile } = await requireClientUser();
|
||||
const displayName = profile.displayName || user.name || user.email.split("@")[0] || "Müşteri";
|
||||
|
||||
if (!user) {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from("profiles")
|
||||
.select("first_name, last_name, avatar_url, role")
|
||||
.eq("id", user.id)
|
||||
.maybeSingle();
|
||||
|
||||
if (profile?.role !== "client") {
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
const fallbackName = user.email?.split("@")[0] ?? "Müşteri";
|
||||
const displayName =
|
||||
[profile?.first_name, profile?.last_name].filter(Boolean).join(" ") ||
|
||||
fallbackName;
|
||||
|
||||
const shortName = displayName
|
||||
.split(" ")
|
||||
.filter(Boolean)
|
||||
.slice(0, 2)
|
||||
.map((part) => part[0]?.toUpperCase())
|
||||
.join("")
|
||||
.slice(0, 2) || "MS";
|
||||
|
||||
const { data: clientData } = await supabase
|
||||
.from("clients")
|
||||
.select("id")
|
||||
.eq("client_auth_id", user.id)
|
||||
.maybeSingle();
|
||||
|
||||
let avgProgress = 0;
|
||||
if (clientData) {
|
||||
const { data: projectsData } = await supabase
|
||||
.from("projects")
|
||||
.select("progress")
|
||||
.eq("client_id", clientData.id)
|
||||
.eq("status", "active");
|
||||
if (projectsData && projectsData.length > 0) {
|
||||
avgProgress = Math.round(projectsData.reduce((sum, p) => sum + p.progress, 0) / projectsData.length);
|
||||
}
|
||||
}
|
||||
const shortName =
|
||||
displayName
|
||||
.split(" ")
|
||||
.filter(Boolean)
|
||||
.slice(0, 2)
|
||||
.map((part) => part[0]?.toUpperCase())
|
||||
.join("")
|
||||
.slice(0, 2) || "MS";
|
||||
|
||||
return (
|
||||
<PortalShell
|
||||
user={{
|
||||
email: user.email ?? "bilinmiyor@mindspace.local",
|
||||
email: user.email,
|
||||
displayName,
|
||||
shortName,
|
||||
avatarUrl: profile?.avatar_url || null,
|
||||
avatarUrl: user.image || null,
|
||||
}}
|
||||
progress={avgProgress}
|
||||
progress={0}
|
||||
>
|
||||
{children}
|
||||
</PortalShell>
|
||||
|
||||
Reference in New Issue
Block a user