mirror of
https://github.com/Since-3/since3-portal.git
synced 2026-10-12 21:41:16 +00:00
46 lines
No EOL
1.2 KiB
TypeScript
46 lines
No EOL
1.2 KiB
TypeScript
import "server-only"
|
|
import { cache } from "react"
|
|
import { redirect } from "next/navigation"
|
|
import { createClient } from "@/lib/supabase/server"
|
|
|
|
|
|
export const getSession = cache(async () => {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
return user
|
|
})
|
|
|
|
export const getProfile = cache(async () => {
|
|
const user = await getSession()
|
|
|
|
if (!user) return null
|
|
|
|
const supabase = await createClient()
|
|
const { data } = await supabase
|
|
.from("profiles")
|
|
.select("role, name, email, terms_accepted_at")
|
|
.eq("id", user.id)
|
|
.single()
|
|
|
|
return data
|
|
})
|
|
|
|
|
|
export const verifySession = cache(async () => {
|
|
const user = await getSession()
|
|
if (!user) redirect("/")
|
|
|
|
const profile = await getProfile();
|
|
if(!profile) redirect("/")
|
|
|
|
return {
|
|
userId: user.id,
|
|
name: profile.name,
|
|
email: profile.email,
|
|
role: profile.role,
|
|
isLoggedIn: true,
|
|
isAdmin: profile.role === "admin",
|
|
isClient: profile.role === "client",
|
|
hasAcceptedTerms: profile.terms_accepted_at != null,
|
|
}
|
|
}) |