Skip to content

Commit 50aa494

Browse files
committed
fix: Enforce stricter types
- Only allow auth enabled collection slugs - Transform adapter objects and payload objects back and forth - Use generated types - Stricter type for the accounts.type field - Make payload session expires field optional
1 parent 22021c4 commit 50aa494

28 files changed

+1079
-384
lines changed

examples/basic/src/payload-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export interface User {
125125
| {
126126
provider: string;
127127
providerAccountId: string;
128-
type: string;
128+
type: 'oidc' | 'oauth' | 'email' | 'webauthn';
129129
id?: string | null;
130130
}[]
131131
| null;

examples/multiple-auth-collections/src/payload-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export interface Customer {
151151
| {
152152
provider: string;
153153
providerAccountId: string;
154-
type: string;
154+
type: 'oidc' | 'oauth' | 'email' | 'webauthn';
155155
id?: string | null;
156156
}[]
157157
| null;
@@ -172,7 +172,7 @@ export interface Admin {
172172
| {
173173
provider: string;
174174
providerAccountId: string;
175-
type: string;
175+
type: 'oidc' | 'oauth' | 'email' | 'webauthn';
176176
id?: string | null;
177177
}[]
178178
| null;

examples/with-payload-local-strategy/src/payload-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export interface User {
124124
| {
125125
provider: string;
126126
providerAccountId: string;
127-
type: string;
127+
type: 'oidc' | 'oauth' | 'email' | 'webauthn';
128128
id?: string | null;
129129
}[]
130130
| null;

packages/dev/src/auth/base.config.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
import jwt from "jsonwebtoken";
22
import type { NextAuthConfig, Session } from "next-auth";
3-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4-
import type { JWT } from "next-auth/jwt";
5-
import type { PayloadAuthjsUser } from "payload-authjs";
6-
import type { User as PayloadUser } from "../payload-types";
7-
8-
declare module "next-auth" {
9-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
10-
interface User extends PayloadAuthjsUser<PayloadUser> {}
11-
}
12-
declare module "next-auth/jwt" {
13-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
14-
interface JWT
15-
extends Partial<
16-
Pick<
17-
PayloadUser,
18-
| "id"
19-
| "additionalUserDatabaseField"
20-
| "additionalUserVirtualField"
21-
| "roles"
22-
| "currentAccount"
23-
>
24-
> {}
25-
}
263

274
export const SESSION_STRATEGY: NonNullable<NonNullable<NextAuthConfig["session"]>["strategy"]> =
285
"jwt";
@@ -46,7 +23,6 @@ export const authConfig: NextAuthConfig = {
4623
if (user.id) {
4724
token.id = user.id;
4825
}
49-
token.additionalUserDatabaseField = user.additionalUserDatabaseField;
5026
}
5127

5228
// Add virtual field to the token
@@ -157,7 +133,6 @@ export const authConfig: NextAuthConfig = {
157133
session.user.id = token.id;
158134
}
159135

160-
session.user.additionalUserDatabaseField = token.additionalUserDatabaseField;
161136
session.user.additionalUserVirtualField = token.additionalUserVirtualField;
162137

163138
session.user.roles = token.roles;

packages/dev/src/auth/types.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable @typescript-eslint/no-empty-object-type */
2+
import type { User as PayloadUser } from "@/payload-types";
3+
import "next-auth/jwt";
4+
import type { PayloadAuthjsUser } from "payload-authjs";
5+
6+
declare module "next-auth" {
7+
interface User extends PayloadAuthjsUser<PayloadUser> {}
8+
}
9+
declare module "next-auth/jwt" {
10+
interface JWT
11+
extends Partial<
12+
Pick<PayloadUser, "id" | "additionalUserVirtualField" | "roles" | "currentAccount">
13+
> {}
14+
}

packages/dev/src/payload-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export interface User {
142142
| {
143143
provider: string;
144144
providerAccountId: string;
145-
type: string;
145+
type: 'oidc' | 'oauth' | 'email' | 'webauthn';
146146
additionalAccountDatabaseField: string;
147147
createdAt: string;
148148
id?: string | null;

packages/payload-authjs/project.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
"input": "packages/payload-authjs/src",
1717
"glob": "**/*.css",
1818
"output": "."
19+
},
20+
{
21+
"input": "packages/payload-authjs/src",
22+
"glob": "**/.npmignore",
23+
"output": "."
1924
}
2025
],
2126
"clean": false
2227
},
23-
"cache": true
28+
"cache": true,
29+
"dependsOn": ["generate:types"]
2430
},
2531
"yalc:publish": {
2632
"executor": "nx:run-commands",
@@ -29,6 +35,16 @@
2935
"command": "pnpx yalc publish"
3036
},
3137
"dependsOn": ["build"]
38+
},
39+
"generate:types": {
40+
"executor": "nx:run-commands",
41+
"options": {
42+
"cwd": "packages/payload-authjs",
43+
"command": "payload generate:types",
44+
"env": {
45+
"PAYLOAD_CONFIG_PATH": "src/types/payload.config.ts"
46+
}
47+
}
3248
}
3349
}
3450
}

0 commit comments

Comments
 (0)