Skip to content

Commit c7e8f08

Browse files
committed
feat: Add PayloadAuthjsUser helper type for authjs module declaration
1 parent 89e749c commit c7e8f08

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

packages/dev/src/auth.config.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type { JWT } from "next-auth/jwt";
55
import github from "next-auth/providers/github";
66
import keycloak from "next-auth/providers/keycloak";
77
import nodemailer from "next-auth/providers/nodemailer";
8+
import type { PayloadAuthjsUser } from "payload-authjs";
89
import type { User as PayloadUser } from "payload/generated-types";
910

1011
declare module "next-auth" {
1112
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
12-
interface User
13-
extends Partial<Omit<PayloadUser, "accounts" | "sessions" | "verificationTokens">> {}
13+
interface User extends PayloadAuthjsUser<PayloadUser> {}
1414
}
1515
declare module "next-auth/jwt" {
1616
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
@@ -145,9 +145,4 @@ export const authConfig: NextAuthConfig = {
145145
return !!auth;
146146
},
147147
},
148-
/* events: {
149-
signIn: () => {
150-
console.log("original events.signIn");
151-
},
152-
}, */
153148
};

packages/payload-authjs/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,23 @@ const Examples: CollectionConfig = {
251251

252252
</details>
253253

254-
## 🎉 Events
254+
## 🟦 Typescript
255+
256+
If you are using typescript you can declare your Auth.js user type as shown in the following example:
257+
258+
```ts
259+
// auth.config.ts
260+
import type { PayloadAuthjsUser } from "payload-authjs";
261+
import type { User as PayloadUser } from "payload/generated-types";
262+
263+
declare module "next-auth" {
264+
interface User extends PayloadAuthjsUser<PayloadUser> {}
265+
}
266+
```
267+
268+
_More information about typescript can be found in the [Auth.js documentation](https://authjs.dev/getting-started/typescript?framework=next-js)._
269+
270+
### 🎉 Events
255271

256272
Auth.js emits some [events](https://authjs.dev/reference/nextjs#events) that you can listen to. This plugin extends the events with additional parameters like the `adapter` and `payload` instance.
257273

@@ -266,7 +282,7 @@ The following events are available:
266282
- linkAccount
267283
- session
268284

269-
## `signIn` Event
285+
### `signIn` Event
270286

271287
The `signIn` event is emitted when a user successfully signs in. For example, you could use this event to update the user's name on every sign-in:
272288

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Helper type to extract the user type from the collection user type.
3+
*
4+
* @example
5+
* import type { PayloadAuthjsUser } from "payload-authjs";
6+
* import type { User as PayloadUser } from "payload/generated-types";
7+
*
8+
* declare module "next-auth" {
9+
* interface User extends PayloadAuthjsUser<PayloadUser> {}
10+
* }
11+
*
12+
* @see https://authjs.dev/getting-started/typescript?framework=next-js#module-augmentation
13+
*/
14+
export type PayloadAuthjsUser<User> = Partial<
15+
Omit<User, "accounts" | "sessions" | "verificationTokens">
16+
>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { PayloadAdapter } from "./authjs/PayloadAdapter";
2+
export { type PayloadAuthjsUser } from "./authjs/types";
23
export { withPayload } from "./authjs/withPayload";
34
export { getPayloadUser } from "./payload/getPayloadUser";
45
export { authjsPlugin, type AuthjsPluginConfig } from "./payload/plugin";

0 commit comments

Comments
 (0)