Skip to content

Commit

Permalink
fix(types): narrowed session type passed to fetch session hook
Browse files Browse the repository at this point in the history
* fix: narrowed session type passed to `fetch` session hook

* fix: type import

* chore: up

---------

Co-authored-by: Sébastien Chopin <seb@nuxt.com>
  • Loading branch information
Gerbuuun and atinux authored Apr 3, 2024
1 parent 50ba6fe commit 77c82e7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { useSession, createError } from 'h3'
import { defu } from 'defu'
import { createHooks } from 'hookable'
import { useRuntimeConfig } from '#imports'
import type { User, UserSession } from '#auth-utils'
import type { UserSession, UserSessionRequired } from '#auth-utils'

export interface SessionHooks {
/**
* Called when fetching the session from the API
* - Add extra properties to the session
* - Throw an error if the session could not be verified (with a database for example)
*/
'fetch': (session: UserSession, event: H3Event) => void | Promise<void>
'fetch': (session: UserSessionRequired, event: H3Event) => void | Promise<void>
/**
* Called before clearing the session
*/
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function clearUserSession (event: H3Event) {
return true
}

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

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

return userSession as UserSession & { user: User }
return userSession as UserSessionRequired
}

let sessionConfig: SessionConfig
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { User, UserSession, UserSessionComposable } from './session'
export type { User, UserSession, UserSessionRequired, UserSessionComposable } from './session'
export type { OAuthConfig } from './oauth-config'
4 changes: 4 additions & 0 deletions src/runtime/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface UserSession {
user?: User
}

export interface UserSessionRequired extends UserSession {
user: User
}

export interface UserSessionComposable {
loggedIn: ComputedRef<boolean>
user: ComputedRef<User | null>
Expand Down

0 comments on commit 77c82e7

Please sign in to comment.