Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event, SessionConfig } from 'h3'
import { useSession, createError, isEvent } from 'h3'
import { defu } from 'defu'
import { createHooks } from 'hookable'
import type { OmitWithIndexSignature } from '../../types/utils'
import { useRuntimeConfig } from '#imports'
import type { UserSession, UserSessionRequired } from '#auth-utils'

Expand Down Expand Up @@ -40,7 +41,7 @@ export async function getUserSession(event: UseSessionEvent): Promise<UserSessio
* @param data User session data, please only store public information since it can be decoded with API calls
* @see https://github.com/atinux/nuxt-auth-utils
*/
export async function setUserSession(event: H3Event, data: Omit<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession> {
export async function setUserSession(event: H3Event, data: OmitWithIndexSignature<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession> {
const session = await _useSession(event, config)

await session.update(defu(data, session.data))
Expand All @@ -53,7 +54,7 @@ export async function setUserSession(event: H3Event, data: Omit<UserSession, 'id
* @param event The Request (h3) event
* @param data User session data, please only store public information since it can be decoded with API calls
*/
export async function replaceUserSession(event: H3Event, data: Omit<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession> {
export async function replaceUserSession(event: H3Event, data: OmitWithIndexSignature<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession> {
const session = await _useSession(event, config)

await session.clear()
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/types/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Prettify<T> = {
[K in keyof T]: T[K]
}

type RemoveIndexSignature<T> = {
[K in keyof T as string extends K ? never : K]: T[K]
}

type GetIndexSignature<T> = {
[K in keyof T as string extends K ? K : never]: T[K]
}

// Basic Omit but does not break if a [x:string]:unknown Index Signature is present in the type
export type OmitWithIndexSignature<T extends Record<string, unknown>, K extends keyof RemoveIndexSignature<T>> = Prettify<Omit<RemoveIndexSignature<T>, K> & GetIndexSignature<T>>