34 lines
753 B
TypeScript
34 lines
753 B
TypeScript
import { cookies } from "next/headers";
|
|
import { db } from "@/lib/db";
|
|
import { sessions, users } from "@/lib/schema";
|
|
import { and, eq, gt } from "drizzle-orm";
|
|
|
|
export async function getUserFromSession() {
|
|
const cookieStore = await cookies();
|
|
const sessionId = cookieStore.get("session")?.value;
|
|
|
|
if (!sessionId) return null;
|
|
|
|
const session = await db
|
|
.select()
|
|
.from(sessions)
|
|
.where(
|
|
and(
|
|
eq(sessions.id, sessionId),
|
|
gt(sessions.expiresAt, new Date())
|
|
)
|
|
)
|
|
.limit(1)
|
|
.then((rows) => rows[0]);
|
|
|
|
if (!session) return null;
|
|
|
|
const user = await db
|
|
.select()
|
|
.from(users)
|
|
.where(eq(users.id, session.userId))
|
|
.limit(1)
|
|
.then((rows) => rows[0]);
|
|
|
|
return user ?? null;
|
|
}
|