Skip to content

Commit d84c361

Browse files
docs(express): add TypeScript guide for custom session properties (#12590)
* docs(express): add TypeScript guide for custom session properties docs(express): add TypeScript guide for custom session properties - Added detailed documentation for extending session types in Express.js - Included code examples for module augmentation and session callback - Explained how to add custom properties like `id` to the session object - Aligned with Auth.js TypeScript best practices and JWT session strategy This update helps Express.js users integrate custom session properties with TypeScript support, following the framework's conventions. * Update docs/pages/getting-started/typescript.mdx --------- Co-authored-by: Thang Vu <hi@thvu.dev>
1 parent b0f1538 commit d84c361

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/pages/getting-started/typescript.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,40 @@ export const { handle } = SvelteKitAuth({
154154
```
155155

156156
</Code.Svelte>
157+
<Code.Express>
158+
```ts filename="auth.ts"
159+
import { ExpressAuthConfig } from "@auth/express";
160+
// Extend the default Session type to include custom properties
161+
declare module "@auth/express" {
162+
interface Session {
163+
user: {
164+
id: string; // Add a custom `id` property to the session user object
165+
};
166+
}
167+
}
168+
169+
export const authConfig: ExpressAuthConfig = {
170+
callbacks: {
171+
/**
172+
* The `session` callback is used to customize the session object
173+
* returned to the client. Here, we add a custom `id` property to
174+
* the session user object, which is populated from the JWT token.
175+
*
176+
* @param session - The current session object.
177+
* @param token - The JWT token containing user information.
178+
* @returns The modified session object with the custom `id` property.
179+
*/
180+
async session({ session, token }) {
181+
if (token.sub) {
182+
// Add the `id` property to the session user object
183+
session.user.id = token.sub; // `token.sub` contains the user ID
184+
}
185+
return session;
186+
},
187+
},
188+
};
189+
```
190+
</Code.Express>
157191
</Code>
158192

159193
Module augmentation is not limited to specific interfaces. You can augment any `interface` we've defined, here are some of the more common interfaces that you might want to override based on your use case.

0 commit comments

Comments
 (0)