diff --git a/docs/content/1.getting-started/1.introduction.md b/docs/content/1.getting-started/1.introduction.md index 4ceeab88..439b0145 100644 --- a/docs/content/1.getting-started/1.introduction.md +++ b/docs/content/1.getting-started/1.introduction.md @@ -120,6 +120,7 @@ Default: include: undefined, exclude: [], saveRedirectToCookie: false, + redirectOnSignOut: false, } ``` @@ -128,6 +129,7 @@ Default: - `include`: Routes to include in the redirect. `['/admin(/*)?']` will enable the redirect only for the `admin` page and all sub-pages. - `exclude`: Routes to exclude from the redirect. `['/foo', '/bar/**']` will exclude the `foo` page and all pages in your `bar` folder. - `saveRedirectToCookie`: Automatically sets a cookie containing the path an unauthenticated user tried to access. The value can be accessed using the [`useSupabaseCookieRedirect`](/composables/usesupabasecookieredirect) composable to redirect the user to the page they previously tried to visit. +- `redirectOnSignOut`: When the session ends while a page is open (sign out, a revoked session or an expired refresh token), re-checks the current route so the user is redirected to the login page instead of staying on a page they can no longer load. Combined with `saveRedirectToCookie`, the user returns to that page after signing in again. ### `cookiePrefix` diff --git a/src/module.ts b/src/module.ts index 75cc9c19..d8ce8fda 100644 --- a/src/module.ts +++ b/src/module.ts @@ -160,6 +160,7 @@ export default defineNuxtModule({ exclude: [], cookieRedirect: false, saveRedirectToCookie: false, + redirectOnSignOut: false, }, cookieName: 'sb', cookiePrefix: undefined, diff --git a/src/runtime/plugins/auth-redirect.ts b/src/runtime/plugins/auth-redirect.ts index 71a934d7..59b574ba 100644 --- a/src/runtime/plugins/auth-redirect.ts +++ b/src/runtime/plugins/auth-redirect.ts @@ -1,7 +1,8 @@ +import { useSupabaseClient } from '../composables/useSupabaseClient' import { useSupabaseCookieRedirect } from '../composables/useSupabaseCookieRedirect' import { useSupabaseSession } from '../composables/useSupabaseSession' import type { Plugin } from '#app' -import { defineNuxtPlugin, addRouteMiddleware, defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo } from '#imports' +import { defineNuxtPlugin, addRouteMiddleware, defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo, useRouter } from '#imports' import type { RouteLocationNormalized } from '#vue-router' function matchesAnyPattern(path: string, patterns: (string | undefined)[]): boolean { @@ -47,5 +48,16 @@ export default defineNuxtPlugin({ }), { global: true }, ) + + // Re-run the middleware on the current route when the session ends, so an open page doesn't stay on screen + if (import.meta.client && useRuntimeConfig().public.supabase.redirectOptions.redirectOnSignOut) { + const router = useRouter() + useSupabaseClient().auth.onAuthStateChange((event) => { + if (event !== 'SIGNED_OUT') return + // Not returned: auth-js awaits callbacks while holding its lock + const { path, query, hash } = router.currentRoute.value + navigateTo({ path, query, hash, force: true }) + }) + } }, }) as Plugin diff --git a/src/types/index.ts b/src/types/index.ts index e0546837..81b26953 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -34,4 +34,10 @@ export interface RedirectOptions { * @default false */ saveRedirectToCookie?: boolean + + /** + * If true, when the session ends (sign out, revoked or expired session) the current route is re-checked, redirecting to the login page unless it is excluded. + * @default false + */ + redirectOnSignOut?: boolean }