Skip to content

Commit 9866498

Browse files
committed
fix(getPayloadUser): Improved type
1 parent 5b8657d commit 9866498

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,13 @@ This plugin also export a utility function to get the current payload user
137137
```tsx
138138
// ServerComponentExample.tsx
139139
const ServerComponentExample = async () => {
140-
const payloadUser = await getPayloadUser<DataFromCollectionSlug<"users">>();
140+
const payloadUser = await getPayloadUser();
141141

142142
return (
143143
<div>
144144
<h3>Payload CMS User</h3>
145-
<div>
146-
{JSON.stringify(payloadUser)}
147-
</div>
145+
<div>{JSON.stringify(payloadUser)}</div>
148146
</div>
149147
);
150148
};
151-
```
149+
```

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { auth } from "@/auth";
2-
import type { DataFromCollectionSlug } from "payload";
32
import { getPayloadUser } from "payload-authjs";
43
import { SignInButton } from "./SignInButton";
54
import { SignOutButtonAuthjs } from "./SignOutButtonAuthjs";
65
import { SignOutButtonPayload } from "./SignOutButtonPayload";
76

87
const AuthOverview = async () => {
98
const session = await auth();
10-
const payloadUser = await getPayloadUser<DataFromCollectionSlug<"users">>();
9+
const payloadUser = await getPayloadUser();
1110

1211
return (
1312
<div>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"use client";
22

3+
import type { CollectionSlug } from "payload";
4+
35
export function SignOutButtonPayload({
46
userCollectionSlug = "users",
57
}: {
6-
userCollectionSlug?: string;
8+
userCollectionSlug?: CollectionSlug;
79
}) {
810
return (
911
<button

src/payload/getPayloadUser.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { cookies } from "next/headers";
2-
import type { CollectionSlug } from "payload";
3-
import type { User } from "./types";
2+
import type { CollectionSlug, DataFromCollectionSlug } from "payload";
43

5-
interface Options {
4+
interface Options<TSlug extends CollectionSlug> {
65
/**
76
* The URL of the server
87
*
@@ -14,16 +13,16 @@ interface Options {
1413
*
1514
* @default "users"
1615
*/
17-
userCollectionSlug?: CollectionSlug;
16+
userCollectionSlug?: TSlug;
1817
}
1918

2019
/**
21-
* Get the user payload from the server (only works on the server side)
20+
* Get the payload user from the server (only works on the server side)
2221
*/
23-
export const getPayloadUser = async <T extends object = User>({
22+
export const getPayloadUser = async <TSlug extends CollectionSlug = "users">({
2423
serverUrl = process.env.NEXT_PUBLIC_SERVER_URL,
25-
userCollectionSlug = "users",
26-
}: Options = {}): Promise<T | null> => {
24+
userCollectionSlug = "users" as TSlug,
25+
}: Options<TSlug> = {}): Promise<DataFromCollectionSlug<TSlug> | null> => {
2726
const requestCookies = await cookies();
2827

2928
if (serverUrl === undefined) {
@@ -38,7 +37,7 @@ export const getPayloadUser = async <T extends object = User>({
3837
},
3938
});
4039

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

4342
if (!meUserReq.ok || !user) {
4443
return null;

0 commit comments

Comments
 (0)