Skip to content

Commit 77c82e7

Browse files
Gerbuuunatinux
andauthored
fix(types): narrowed session type passed to fetch session hook
* fix: narrowed session type passed to `fetch` session hook * fix: type import * chore: up --------- Co-authored-by: Sébastien Chopin <seb@nuxt.com>
1 parent 50ba6fe commit 77c82e7

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/runtime/server/utils/session.ts

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

88
export interface SessionHooks {
99
/**
1010
* Called when fetching the session from the API
1111
* - Add extra properties to the session
1212
* - Throw an error if the session could not be verified (with a database for example)
1313
*/
14-
'fetch': (session: UserSession, event: H3Event) => void | Promise<void>
14+
'fetch': (session: UserSessionRequired, event: H3Event) => void | Promise<void>
1515
/**
1616
* Called before clearing the session
1717
*/
@@ -59,7 +59,7 @@ export async function clearUserSession (event: H3Event) {
5959
return true
6060
}
6161

62-
export async function requireUserSession(event: H3Event): Promise<UserSession & { user: User }> {
62+
export async function requireUserSession(event: H3Event): Promise<UserSessionRequired> {
6363
const userSession = await getUserSession(event)
6464

6565
if (!userSession.user) {
@@ -69,7 +69,7 @@ export async function requireUserSession(event: H3Event): Promise<UserSession &
6969
})
7070
}
7171

72-
return userSession as UserSession & { user: User }
72+
return userSession as UserSessionRequired
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 { User, UserSession, UserSessionComposable } from './session'
1+
export type { User, UserSession, UserSessionRequired, UserSessionComposable } from './session'
22
export type { OAuthConfig } from './oauth-config'

src/runtime/types/session.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface UserSession {
77
user?: User
88
}
99

10+
export interface UserSessionRequired extends UserSession {
11+
user: User
12+
}
13+
1014
export interface UserSessionComposable {
1115
loggedIn: ComputedRef<boolean>
1216
user: ComputedRef<User | null>

0 commit comments

Comments
 (0)