diff --git a/README.md b/README.md index 52a10339..e330b49e 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ interface UserSessionComposable { /** * Clear the user session and remove the session cookie. */ - clear: () => Promise + clear: (options?: ClearSessionOptions) => Promise /** * Open the OAuth route in a popup that auto-closes when successful. */ diff --git a/src/runtime/app/composables/session.ts b/src/runtime/app/composables/session.ts index eadc7c9f..bd35704a 100644 --- a/src/runtime/app/composables/session.ts +++ b/src/runtime/app/composables/session.ts @@ -1,6 +1,6 @@ import { appendResponseHeader } from 'h3' -import { useState, computed, useRequestFetch, useRequestEvent } from '#imports' -import type { UserSession, UserSessionComposable } from '#auth-utils' +import { useState, computed, useRequestFetch, useRequestEvent, useRouter } from '#imports' +import type { UserSession, UserSessionComposable, ClearSessionOptions } from '#auth-utils' /** * Composable to get back the user session and utils around it. @@ -10,8 +10,9 @@ export function useUserSession(): UserSessionComposable { const serverEvent = import.meta.server ? useRequestEvent() : null const sessionState = useState('nuxt-session', () => ({})) const authReadyState = useState('nuxt-auth-ready', () => false) + const router = useRouter() - const clear = async () => { + const clear = async (options?: ClearSessionOptions) => { await useRequestFetch()('/api/_auth/session', { method: 'DELETE', onResponse({ response: { headers } }) { @@ -24,6 +25,9 @@ export function useUserSession(): UserSessionComposable { }, }) sessionState.value = {} + if (options?.redirect) { + router.push(options.redirect) + } } const fetch = async () => { diff --git a/src/runtime/types/index.ts b/src/runtime/types/index.ts index 31ea069b..48ce5927 100644 --- a/src/runtime/types/index.ts +++ b/src/runtime/types/index.ts @@ -1,4 +1,4 @@ -export type { User, UserSession, UserSessionRequired, UserSessionComposable, SecureSessionData } from './session' +export type { User, UserSession, UserSessionRequired, UserSessionComposable, SecureSessionData, ClearSessionOptions } from './session' export type { OAuthConfig, OAuthProvider, ATProtoProvider, OnError } from './oauth-config' export type { WebAuthnCredential, diff --git a/src/runtime/types/session.ts b/src/runtime/types/session.ts index 698d7d0e..a9133d2e 100644 --- a/src/runtime/types/session.ts +++ b/src/runtime/types/session.ts @@ -30,6 +30,10 @@ export interface UserSessionRequired extends UserSession { user: User } +export interface ClearSessionOptions { + redirect?: string +} + export interface UserSessionComposable { /** * Computed indicating if the auth session is ready @@ -54,7 +58,7 @@ export interface UserSessionComposable { /** * Clear the user session and remove the session cookie. */ - clear: () => Promise + clear: (options?: ClearSessionOptions) => Promise /** * Open the OAuth route in a popup that auto-closes when successful. */