You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
// Extend the default Session type to include custom properties
161
+
declaremodule"@auth/express" {
162
+
interfaceSession {
163
+
user: {
164
+
id:string; // Add a custom `id` property to the session user object
165
+
};
166
+
}
167
+
}
168
+
169
+
exportconst 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
+
* @paramsession - The current session object.
177
+
* @paramtoken - 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
+
returnsession;
186
+
},
187
+
},
188
+
};
189
+
```
190
+
</Code.Express>
157
191
</Code>
158
192
159
193
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