Skip to content

Commit c06262b

Browse files
committed
fix(AuthjsAuthStrategy): Respect auth.depth option of users collection
1 parent da23a5a commit c06262b

File tree

6 files changed

+41
-55
lines changed

6 files changed

+41
-55
lines changed

packages/dev/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@payloadcms/ui": "3.31.0",
2020
"@tailwindcss/postcss": "^4.0.16",
2121
"clsx": "^2.1.1",
22+
"graphql": "^16.10.0",
2223
"jsonwebtoken": "^9.0.2",
2324
"next": "15.2.4",
2425
"next-auth": "5.0.0-beta.25",
@@ -33,6 +34,7 @@
3334
"tailwindcss": "^4.0.16"
3435
},
3536
"devDependencies": {
37+
"@payloadcms/graphql": "3.31.0",
3638
"@types/jsonwebtoken": "^9.0.9",
3739
"@types/react": "19.0.12",
3840
"dotenv": "^16.4.7",

packages/dev/src/payload-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export interface User {
173173
export interface Example {
174174
id: number;
175175
someField?: string | null;
176+
author?: (string | null) | User;
176177
updatedAt: string;
177178
createdAt: string;
178179
}
@@ -293,6 +294,7 @@ export interface UsersSelect<T extends boolean = true> {
293294
*/
294295
export interface ExamplesSelect<T extends boolean = true> {
295296
someField?: T;
297+
author?: T;
296298
updatedAt?: T;
297299
createdAt?: T;
298300
}

packages/dev/src/payload/collections/examples.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CollectionConfig } from "payload";
22
import { hasRole } from "../access/hasRole";
33

4-
const Examples: CollectionConfig = {
4+
const Examples: CollectionConfig<"examples"> = {
55
slug: "examples",
66
admin: {
77
useAsTitle: "someField",
@@ -17,6 +17,12 @@ const Examples: CollectionConfig = {
1717
name: "someField",
1818
type: "text",
1919
},
20+
{
21+
name: "author",
22+
type: "relationship",
23+
relationTo: "users",
24+
maxDepth: 0,
25+
},
2026
],
2127
};
2228

packages/dev/src/payload/collections/users.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SESSION_STRATEGY } from "@/auth/base.config";
22
import type { CollectionConfig, Field } from "payload";
33
import { createdAtField } from "../fields/createdAt";
44

5-
const Users: CollectionConfig = {
5+
const Users: CollectionConfig<"users"> = {
66
slug: "users",
77
admin: {
88
useAsTitle: "name",
@@ -14,6 +14,7 @@ const Users: CollectionConfig = {
1414
},
1515
auth: {
1616
useAPIKey: true,
17+
depth: 0,
1718
},
1819
fields: [
1920
{
@@ -164,6 +165,12 @@ const Users: CollectionConfig = {
164165
}),
165166
],
166167
},
168+
/* {
169+
name: "examples",
170+
type: "join",
171+
collection: "examples",
172+
on: "author",
173+
}, */
167174
],
168175
/* hooks: {
169176
afterLogout: [

packages/payload-authjs/src/payload/AuthjsAuthStrategy.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function AuthjsAuthStrategy(
2020

2121
return {
2222
name: AUTHJS_STRATEGY_NAME,
23-
authenticate: async ({ payload }) => {
23+
authenticate: async ({ payload, isGraphQL }) => {
2424
// Get session from authjs
2525
const { auth } = NextAuth(
2626
withPayload(pluginOptions.authjsConfig, {
@@ -36,6 +36,7 @@ export function AuthjsAuthStrategy(
3636
}
3737

3838
// Find user in database
39+
const sanitizedCollectionConfig = payload.collections[collection.slug].config;
3940
const payloadUser = (
4041
await payload.find({
4142
collection: collection.slug,
@@ -48,6 +49,8 @@ export function AuthjsAuthStrategy(
4849
equals: session.user.email,
4950
},
5051
},
52+
depth: isGraphQL ? 0 : sanitizedCollectionConfig.auth.depth,
53+
limit: 1,
5154
})
5255
).docs.at(0);
5356

pnpm-lock.yaml

Lines changed: 18 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)