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

fix: narrowed session type passed to fetch session hook #71

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
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
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
Loading