Skip to content

Commit 1b99533

Browse files
Gerbuuunatinux
andauthored
fix: UserSession user type augmentation (#54)
* feat: create User interface for module augmentation * fix: user is no longer possibly undefined when using `requireUserSession` * chore: update docs --------- Co-authored-by: Sébastien Chopin <[email protected]>
1 parent 58268d7 commit 1b99533

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,15 @@ You can define the type for your user session by creating a type declaration fil
116116

117117
```ts
118118
declare module '#auth-utils' {
119+
interface User {
120+
// Add your own fields
121+
}
122+
119123
interface UserSession {
120-
// define the type here
124+
// Add your own fields
121125
}
122126
}
127+
123128
export {}
124129
```
125130

playground/auth.d.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
declare module '#auth-utils' {
2+
interface User {
3+
spotify?: any
4+
github?: any
5+
google?: any
6+
twitch?: any
7+
auth0?: any
8+
microsoft?: any;
9+
discord?: any
10+
battledotnet?: any
11+
keycloak?: any
12+
linkedin?: any
13+
}
14+
215
interface UserSession {
3-
user: {
4-
spotify?: any
5-
github?: any
6-
google?: any
7-
twitch?: any
8-
auth0?: any
9-
microsoft?: any;
10-
discord?: any
11-
battledotnet?: any
12-
keycloak?: any
13-
linkedin?: any
14-
}
1516
extended?: any
1617
loggedInAt: number
1718
}

src/runtime/server/utils/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useSession, createError } from 'h3'
33
import { defu } from 'defu'
44
import { createHooks } from 'hookable'
55
import { useRuntimeConfig } from '#imports'
6-
import type { UserSession } from '#auth-utils'
6+
import type { User, UserSession } from '#auth-utils'
77

88
export interface SessionHooks {
99
/**
@@ -69,7 +69,7 @@ export async function requireUserSession(event: H3Event) {
6969
})
7070
}
7171

72-
return userSession
72+
return userSession as UserSession & { user: User }
7373
}
7474

7575
let sessionConfig: SessionConfig

src/runtime/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export type { UserSession } from './session'
1+
export type { UserSession, User } from './session'
22
export type { OAuthConfig } from './oauth-config'

src/runtime/types/session.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export interface User {
2+
}
3+
14
export interface UserSession {
2-
user?: {}
5+
user?: User
36
}

0 commit comments

Comments
 (0)