Skip to content

Commit

Permalink
fix: user session types (#55)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Gerbuuun and atinux authored Feb 17, 2024
1 parent a814b58 commit 6a6043a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/runtime/composables/session.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useState, computed, useRequestFetch } from '#imports'
import type { UserSession } from '#auth-utils'
import type { UserSession, UserSessionComposable } from '#auth-utils'

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

export const useUserSession = () => {
export function useUserSession(): UserSessionComposable {
const sessionState = useSessionState()
return {
loggedIn: computed(() => Boolean(sessionState.value.user)),
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function clearUserSession (event: H3Event) {
return true
}

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

if (!userSession.user) {
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 { UserSession, User } from './session'
export type { User, UserSession, UserSessionComposable } from './session'
export type { OAuthConfig } from './oauth-config'
10 changes: 10 additions & 0 deletions src/runtime/types/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { ComputedRef, Ref } from 'vue'

export interface User {
}

export interface UserSession {
user?: User
}

export interface UserSessionComposable {
loggedIn: ComputedRef<boolean>
user: ComputedRef<User | null>
session: Ref<UserSession>,
fetch: () => Promise<void>,
clear: () => Promise<void>
}

0 comments on commit 6a6043a

Please sign in to comment.