Skip to content

Commit ef516b6

Browse files
committed
fix: Fix types of getPayloadUser
1 parent bf0680a commit ef516b6

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

dev/src/app/(app)/_components/AuthOverview.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { auth } from "@/auth";
2+
import { DataFromCollectionSlug } from "payload";
23
import { getPayloadUser } from "../../../../../src";
34
import { SignInButton } from "./SignInButton";
45
import { SignOutButton } from "./SignOutButton";
56

67
const AuthOverview = async () => {
78
const session = await auth();
8-
const payloadUser = await getPayloadUser();
9+
const payloadUser = await getPayloadUser<DataFromCollectionSlug<"users">>();
910

1011
return (
1112
<div>

src/PayloadAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
AdapterUser,
66
VerificationToken as AdapterVerificationToken,
77
} from "next-auth/adapters";
8-
import { Payload, SanitizedConfig } from "payload";
8+
import { CollectionSlug, Payload, SanitizedConfig } from "payload";
99
import { Session, User, VerificationToken } from "./types";
1010

1111
export interface PayloadAdapterOptions {
@@ -25,7 +25,7 @@ export interface PayloadAdapterOptions {
2525
*
2626
* @default "users"
2727
*/
28-
userCollectionSlug?: string;
28+
userCollectionSlug?: CollectionSlug;
2929
}
3030
/**
3131
* Auth.js Database Adapter for Payload CMS

src/getPayloadUser.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
import { cookies } from "next/headers";
2-
import { CollectionSlug, DataFromCollectionSlug } from "payload";
2+
import { CollectionSlug } from "payload";
3+
import { User } from "./types";
34

4-
type Options<TSlug extends CollectionSlug> = {
5+
type Options = {
6+
/**
7+
* The URL of the server
8+
*
9+
* @default process.env.NEXT_PUBLIC_SERVER_URL
10+
*/
11+
serverUrl?: string;
512
/**
613
* The slug of the collection that contains the users
714
*
815
* @default "users"
916
*/
10-
userCollectionSlug?: TSlug;
17+
userCollectionSlug?: CollectionSlug;
1118
};
1219

13-
export const getPayloadUser = async <TSlug extends CollectionSlug = "users">({
14-
userCollectionSlug = "users" as TSlug,
15-
}: Options<TSlug> = {}) => {
20+
/**
21+
* Get the user payload from the server (only works on the server side)
22+
*/
23+
export const getPayloadUser = async <T extends object = User>({
24+
serverUrl = process.env.NEXT_PUBLIC_SERVER_URL,
25+
userCollectionSlug = "users",
26+
}: Options = {}) => {
27+
if (serverUrl === undefined) {
28+
throw new Error(
29+
"getPayloadUser requires a server URL to be provided, either as an option or in the 'NEXT_PUBLIC_SERVER_URL' environment variable",
30+
);
31+
}
32+
1633
const cookieStore = cookies();
1734

18-
const meUserReq = await fetch(
19-
`${process.env.NEXT_PUBLIC_SERVER_URL}/api/${userCollectionSlug}/me`,
20-
{
21-
headers: {
22-
Cookie: cookieStore.toString(),
23-
},
35+
const meUserReq = await fetch(`${serverUrl}/api/${userCollectionSlug}/me`, {
36+
headers: {
37+
Cookie: cookieStore.toString(),
2438
},
25-
);
39+
});
2640

27-
const { user }: { user: DataFromCollectionSlug<TSlug> } = await meUserReq.json();
41+
const { user }: { user: T } = await meUserReq.json();
2842

2943
if (!meUserReq.ok || !user) return;
3044

0 commit comments

Comments
 (0)