Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(express): add TypeScript guide for custom session properties #12590

Merged
merged 3 commits into from
Feb 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/pages/getting-started/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,40 @@ export const { handle } = SvelteKitAuth({
```

</Code.Svelte>
<Code.Express>
```ts filename="auth.ts"
import { ExpressAuthConfig } from "@auth/express";
// Extend the default Session type to include custom properties
declare module "@auth/express" {
interface Session {
user: {
id: string; // Add a custom `id` property to the session user object
};
}
}

export const authConfig: ExpressAuthConfig = {
callbacks: {
/**
* The `session` callback is used to customize the session object
* returned to the client. Here, we add a custom `id` property to
* the session user object, which is populated from the JWT token.
*
* @param session - The current session object.
* @param token - The JWT token containing user information.
* @returns The modified session object with the custom `id` property.
*/
async session({ session, token }) {
if (token.sub) {
// Add the `id` property to the session user object
session.user.id = token.sub; // `token.sub` contains the user ID
}
return session;
},
},
};
```
</Code.Express>
</Code>

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.
Expand Down
Loading