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
2 changes: 2 additions & 0 deletions docs/content/1.getting-started/1.introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Default:
include: undefined,
exclude: [],
saveRedirectToCookie: false,
redirectOnSignOut: false,
}
```

Expand All @@ -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`

Expand Down
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export default defineNuxtModule<ModuleOptions>({
exclude: [],
cookieRedirect: false,
saveRedirectToCookie: false,
redirectOnSignOut: false,
},
cookieName: 'sb',
cookiePrefix: undefined,
Expand Down
14 changes: 13 additions & 1 deletion src/runtime/plugins/auth-redirect.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading