Skip to content

Commit 6a6043a

Browse files
Gerbuuunatinux
andauthored
fix: user session types (#55)
* feat: create User interface for module augmentation * fix: user is no longer possibly undefined when using `requireUserSession` * chore: update docs * fix: explicitly define returntypes * up --------- Co-authored-by: Sébastien Chopin <seb@nuxt.com>
1 parent a814b58 commit 6a6043a

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

src/runtime/composables/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useState, computed, useRequestFetch } from '#imports'
2-
import type { UserSession } from '#auth-utils'
2+
import type { UserSession, UserSessionComposable } from '#auth-utils'
33

44
const useSessionState = () => useState<UserSession>('nuxt-session', () => ({}))
55

6-
export const useUserSession = () => {
6+
export function useUserSession(): UserSessionComposable {
77
const sessionState = useSessionState()
88
return {
99
loggedIn: computed(() => Boolean(sessionState.value.user)),

src/runtime/server/utils/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function clearUserSession (event: H3Event) {
5959
return true
6060
}
6161

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

6565
if (!userSession.user) {

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, User } from './session'
1+
export type { User, UserSession, UserSessionComposable } from './session'
22
export type { OAuthConfig } from './oauth-config'

src/runtime/types/session.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import type { ComputedRef, Ref } from 'vue'
2+
13
export interface User {
24
}
35

46
export interface UserSession {
57
user?: User
68
}
9+
10+
export interface UserSessionComposable {
11+
loggedIn: ComputedRef<boolean>
12+
user: ComputedRef<User | null>
13+
session: Ref<UserSession>,
14+
fetch: () => Promise<void>,
15+
clear: () => Promise<void>
16+
}

0 commit comments

Comments
 (0)