Link clocked time to invoice items with bidirectional sync, add entry editing on web, broaden session cookie detection for Expo clients, and handle API rate limits without signing users out. Co-authored-by: Cursor <cursoragent@cursor.com>
32 lines
859 B
TypeScript
32 lines
859 B
TypeScript
import { headers as nextHeaders } from "next/headers";
|
|
import { auth } from "~/lib/auth";
|
|
|
|
export function hasSessionCookie(headers: Headers): boolean {
|
|
const cookie = headers.get("cookie") ?? "";
|
|
if (!cookie.trim()) return false;
|
|
|
|
return (
|
|
cookie.includes("session_token=") ||
|
|
cookie.includes("session_data=") ||
|
|
cookie.includes("better-auth.session_token=") ||
|
|
cookie.includes("__Secure-better-auth.session_token=")
|
|
);
|
|
}
|
|
|
|
export async function getOptionalServerSession(headers: Headers) {
|
|
if (!hasSessionCookie(headers)) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return await auth.api.getSession({ headers });
|
|
} catch (error) {
|
|
console.error("[auth] Failed to resolve session:", error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getOptionalServerSessionFromHeaders() {
|
|
return getOptionalServerSession(await nextHeaders());
|
|
}
|