payload-authjs
Version:
A Payload CMS 3 plugin for Auth.js 5
54 lines (53 loc) • 2.25 kB
JavaScript
import { headers } from "next/headers";
import { cache } from "react";
/**
* Get the payload session from the server-side
*
* This function is cached to de-duplicate requests:
* - using React 'cache' function to memorize within the same request (@see https://react.dev/reference/react/cache)
* - and using Next.js 'data cache' to cache across multiple requests (@see https://nextjs.org/docs/app/building-your-application/caching#data-cache)
*
* You can manually invalidate the cache by calling `revalidateTag("payload-session")`
*/ export const getPayloadSession = cache(async ({ userCollectionSlug = "users" } = {})=>{
// Get the server URL
const serverUrl = await getServerUrl();
// Fetch the session from the server
const requestHeaders = await headers();
const response = await fetch(`${serverUrl}/api/${userCollectionSlug}/me`, {
headers: new Headers({
cookie: requestHeaders.get("cookie") ?? ""
}),
cache: "force-cache",
next: {
tags: [
"payload-session"
]
}
});
const result = await response.json();
// If the response is not ok or the user is not present, return null
if (!response.ok || !result.user) {
return null;
}
// Return the session
return {
user: result.user,
expires: result.exp && typeof result.exp === "number" ? new Date(result.exp * 1000).toISOString() : undefined,
collection: result.collection ?? result.user.collection,
strategy: result.user._strategy ?? result.strategy
};
});
/**
* Get the server URL from the environment variables or the request headers
*/ const getServerUrl = async ()=>{
let serverUrl = process.env.NEXT_PUBLIC_SERVER_URL;
if (!serverUrl) {
const requestHeaders = await headers();
const detectedHost = requestHeaders.get("x-forwarded-host") ?? requestHeaders.get("host");
const detectedProtocol = requestHeaders.get("x-forwarded-proto") ?? "https";
const protocol = detectedProtocol.endsWith(":") ? detectedProtocol : detectedProtocol + ":";
serverUrl = `${protocol}//${detectedHost}`;
}
return serverUrl;
};
//# sourceMappingURL=getPayloadSession.js.map