Skip to content

feat: redirect option after clearing #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface UserSessionComposable {
/**
* Clear the user session and remove the session cookie.
*/
clear: () => Promise<void>
clear: (options?: ClearSessionOptions) => Promise<void>
/**
* Open the OAuth route in a popup that auto-closes when successful.
*/
Expand Down
10 changes: 7 additions & 3 deletions src/runtime/app/composables/session.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -10,8 +10,9 @@ export function useUserSession(): UserSessionComposable {
const serverEvent = import.meta.server ? useRequestEvent() : null
const sessionState = useState<UserSession>('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 } }) {
Expand All @@ -24,6 +25,9 @@ export function useUserSession(): UserSessionComposable {
},
})
sessionState.value = {}
if (options?.redirect) {
router.push(options.redirect)
}
}

const fetch = async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,7 +58,7 @@ export interface UserSessionComposable {
/**
* Clear the user session and remove the session cookie.
*/
clear: () => Promise<void>
clear: (options?: ClearSessionOptions) => Promise<void>
/**
* Open the OAuth route in a popup that auto-closes when successful.
*/
Expand Down
Loading