From a884073de17a28c2c4ce71c337c6ecd6c8ee0e0f Mon Sep 17 00:00:00 2001 From: Gerben Date: Mon, 12 Feb 2024 18:11:09 +0100 Subject: [PATCH 1/5] feat: create User interface for module augmentation --- playground/auth.d.ts | 25 +++++++++++++------------ src/runtime/types/index.ts | 2 +- src/runtime/types/session.ts | 5 ++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/playground/auth.d.ts b/playground/auth.d.ts index cb3e6a98..c873254a 100644 --- a/playground/auth.d.ts +++ b/playground/auth.d.ts @@ -1,17 +1,18 @@ declare module '#auth-utils' { + interface User { + spotify?: any + github?: any + google?: any + twitch?: any + auth0?: any + microsoft?: any; + discord?: any + battledotnet?: any + keycloak?: any + linkedin?: any + } + interface UserSession { - user: { - spotify?: any - github?: any - google?: any - twitch?: any - auth0?: any - microsoft?: any; - discord?: any - battledotnet?: any - keycloak?: any - linkedin?: any - } extended?: any loggedInAt: number } diff --git a/src/runtime/types/index.ts b/src/runtime/types/index.ts index 602def1a..2717a6e4 100644 --- a/src/runtime/types/index.ts +++ b/src/runtime/types/index.ts @@ -1,2 +1,2 @@ -export type { UserSession } from './session' +export type { UserSession, User } from './session' export type { OAuthConfig } from './oauth-config' diff --git a/src/runtime/types/session.ts b/src/runtime/types/session.ts index 1f52a188..f2eb8500 100644 --- a/src/runtime/types/session.ts +++ b/src/runtime/types/session.ts @@ -1,3 +1,6 @@ +export interface User { +} + export interface UserSession { - user?: {} + user?: User } From 9ae516d5e7b49e38d4cc8532abef348fcaf905a6 Mon Sep 17 00:00:00 2001 From: Gerben Date: Mon, 12 Feb 2024 18:19:20 +0100 Subject: [PATCH 2/5] fix: user is no longer possibly undefined when using `requireUserSession` --- src/runtime/server/utils/session.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/server/utils/session.ts b/src/runtime/server/utils/session.ts index 2f5b1c9f..d15216a9 100644 --- a/src/runtime/server/utils/session.ts +++ b/src/runtime/server/utils/session.ts @@ -3,7 +3,7 @@ import { useSession, createError } from 'h3' import { defu } from 'defu' import { createHooks } from 'hookable' import { useRuntimeConfig } from '#imports' -import type { UserSession } from '#auth-utils' +import type { User, UserSession } from '#auth-utils' export interface SessionHooks { /** @@ -55,7 +55,7 @@ export async function requireUserSession(event: H3Event) { }) } - return userSession + return userSession as UserSession & { user: User } } let sessionConfig: SessionConfig From 6416b5b702a7d0ab33d8f167384dba1440ad4d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Fri, 16 Feb 2024 13:01:16 +0100 Subject: [PATCH 3/5] chore: update docs --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8fe3ae69..9e0ebdfd 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,15 @@ You can define the type for your user session by creating a type declaration fil ```ts declare module '#auth-utils' { + interface User { + // Add your own fields + } + interface UserSession { - // define the type here + // Add your own fields } } + export {} ``` From f7c6704d2143de827446ebcf3af21289c5bef70d Mon Sep 17 00:00:00 2001 From: Gerben Date: Fri, 16 Feb 2024 15:53:36 +0100 Subject: [PATCH 4/5] fix: explicitly define returntypes --- src/runtime/composables/session.ts | 4 ++-- src/runtime/server/utils/session.ts | 2 +- src/runtime/types/index.ts | 2 +- src/runtime/types/session.ts | 10 ++++++++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/runtime/composables/session.ts b/src/runtime/composables/session.ts index 0c73ee9e..e3daabe3 100644 --- a/src/runtime/composables/session.ts +++ b/src/runtime/composables/session.ts @@ -1,9 +1,9 @@ import { useState, computed, useRequestFetch } from '#imports' -import type { UserSession } from '#auth-utils' +import type { UserSession, UserSessionApi } from '#auth-utils' const useSessionState = () => useState('nuxt-session', () => ({})) -export const useUserSession = () => { +export function useUserSession(): UserSessionApi { const sessionState = useSessionState() return { loggedIn: computed(() => Boolean(sessionState.value.user)), diff --git a/src/runtime/server/utils/session.ts b/src/runtime/server/utils/session.ts index d15216a9..00219b40 100644 --- a/src/runtime/server/utils/session.ts +++ b/src/runtime/server/utils/session.ts @@ -45,7 +45,7 @@ export async function clearUserSession (event: H3Event) { return true } -export async function requireUserSession(event: H3Event) { +export async function requireUserSession(event: H3Event): Promise { const userSession = await getUserSession(event) if (!userSession.user) { diff --git a/src/runtime/types/index.ts b/src/runtime/types/index.ts index 2717a6e4..69069eb2 100644 --- a/src/runtime/types/index.ts +++ b/src/runtime/types/index.ts @@ -1,2 +1,2 @@ -export type { UserSession, User } from './session' +export type { User, UserSession, UserSessionApi } from './session' export type { OAuthConfig } from './oauth-config' diff --git a/src/runtime/types/session.ts b/src/runtime/types/session.ts index f2eb8500..b3ede7c8 100644 --- a/src/runtime/types/session.ts +++ b/src/runtime/types/session.ts @@ -1,6 +1,16 @@ +import type { ComputedRef, Ref } from 'vue' + export interface User { } export interface UserSession { user?: User } + +export interface UserSessionApi { + loggedIn: ComputedRef + user: ComputedRef + session: Ref, + fetch: () => Promise, + clear: () => Promise +} \ No newline at end of file From efbc92b3aaf524ce4d753de604c08be5b2241591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sat, 17 Feb 2024 15:57:42 +0100 Subject: [PATCH 5/5] up --- src/runtime/composables/session.ts | 4 ++-- src/runtime/types/index.ts | 2 +- src/runtime/types/session.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/runtime/composables/session.ts b/src/runtime/composables/session.ts index e3daabe3..ac4735c9 100644 --- a/src/runtime/composables/session.ts +++ b/src/runtime/composables/session.ts @@ -1,9 +1,9 @@ import { useState, computed, useRequestFetch } from '#imports' -import type { UserSession, UserSessionApi } from '#auth-utils' +import type { UserSession, UserSessionComposable } from '#auth-utils' const useSessionState = () => useState('nuxt-session', () => ({})) -export function useUserSession(): UserSessionApi { +export function useUserSession(): UserSessionComposable { const sessionState = useSessionState() return { loggedIn: computed(() => Boolean(sessionState.value.user)), diff --git a/src/runtime/types/index.ts b/src/runtime/types/index.ts index 69069eb2..e0ea19bc 100644 --- a/src/runtime/types/index.ts +++ b/src/runtime/types/index.ts @@ -1,2 +1,2 @@ -export type { User, UserSession, UserSessionApi } from './session' +export type { User, UserSession, UserSessionComposable } from './session' export type { OAuthConfig } from './oauth-config' diff --git a/src/runtime/types/session.ts b/src/runtime/types/session.ts index b3ede7c8..3a09073b 100644 --- a/src/runtime/types/session.ts +++ b/src/runtime/types/session.ts @@ -7,10 +7,10 @@ export interface UserSession { user?: User } -export interface UserSessionApi { +export interface UserSessionComposable { loggedIn: ComputedRef user: ComputedRef session: Ref, fetch: () => Promise, clear: () => Promise -} \ No newline at end of file +}