Skip to content

Commit f9aad55

Browse files
authored
feat(auth-middleware): add include to RedirectOptions (#336)
Resolves #268
1 parent 5c1df87 commit f9aad55

File tree

7 files changed

+21
-3
lines changed

7 files changed

+21
-3
lines changed

docs/content/2.get-started.md

+2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ Default:
9898
redirectOptions: {
9999
login: '/login',
100100
callback: '/confirm',
101+
include: undefined,
101102
exclude: [],
102103
cookieRedirect: false,
103104
}
104105
```
105106
106107
- `login`: User will be redirected to this path if not authenticated or after logout.
107108
- `callback`: This is the path the user will be redirect to after supabase login redirection. Should match configured `redirectTo` option of your [signIn method](https://supabase.com/docs/reference/javascript/auth-signinwithoauth). Should also be configured in your Supabase dashboard under `Authentication -> URL Configuration -> Redirect URLs`.
109+
- `include`: Routes to include in the redirect. `['/admin(/*)?']` will enable the redirect only for the `admin` page and all sub-pages.
108110
- `exclude`: Routes to exclude from the redirect. `['/foo', '/bar/*']` will exclude the `foo` page and all pages in your `bar` folder.
109111
- `cookieRedirect`: Sets a cookie containing the path an unauthenticated user tried to access. The cookie can then be used on the [`/confirm`](https://supabase.nuxtjs.org/authentication#confirm-page-confirm) page to redirect the user to the page they previously tried to visit.
110112

docs/content/3.authentication.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ For advanced users who want to implement the auth behaviour themselves, you can
1414

1515
## Log-in page - `/login`
1616

17-
Each time a user is trying to access a page that needs authentication, he will automatically be redirected to the configured log in page. If you want to allow access to "public" page, you just need to add them in the [exclude](/get-started#redirectoptions) redirect option.
17+
Each time a user is trying to access a page that needs authentication, he will automatically be redirected to the configured log in page. If you want to allow access to "public" page, you just need to add them in the [exclude](/get-started#redirectoptions) redirect option. Alternatively, you can enable the redirect only for certain routes using the [include](/get-started#redirectoptions) redirect option.
1818

1919
::callout{icon="i-heroicons-exclamation-triangle-20-solid" color="amber"}
2020
Ensure to activate the authentication providers you want in the Supabase Dashboard under `Authentication -> Providers`.

docs/content/4.usage/composables/useSupabaseUser.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const user = useSupabaseUser()
1414
## Auth middleware
1515

1616
::callout{icon="i-heroicons-light-bulb"}
17-
By default, the module is implementing a redirect middleware. All pages of your application are automatically redirected to the [login](/get-started#redirectoptions) page. However, you can allow redirection to "public" pages by setting the [exclude](/get-started#redirectoptions) redirect option.
17+
By default, the module is implementing a redirect middleware. All pages of your application are automatically redirected to the [login](/get-started#redirectoptions) page. However, you can allow redirection to "public" pages by setting the [exclude](/get-started#redirectoptions) redirect option. Alternatively, you can enable the redirect only for certain routes using the [include](/get-started#redirectoptions) redirect option.
1818
::
1919

2020
If the [redirect](/get-started#redirect) option is disabled, you can protect your authenticated routes by creating a custom middleware in your project, here is an example:

playground/nuxt.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default defineNuxtConfig({
1717
redirectOptions: {
1818
login: '/login',
1919
callback: '/confirm',
20+
// include: ['/protected'],
2021
exclude: ['/unprotected', '/public/*']
2122
}
2223
},

playground/pages/protected.vue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>This page is protected. You should only be able to access it, if you are logged in.</div>
3+
</template>

src/runtime/plugins/auth-redirect.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ export default defineNuxtPlugin({
88
'global-auth',
99
defineNuxtRouteMiddleware((to) => {
1010
const config = useRuntimeConfig().public.supabase
11-
const { login, callback, exclude, cookieRedirect } = config.redirectOptions
11+
const { login, callback, include, exclude, cookieRedirect } = config.redirectOptions
1212
const { cookieName, cookieOptions } = config
1313

14+
// Redirect only on included routes (if defined)
15+
if (include && include.length > 0) {
16+
const isIncluded = include.some((path) => {
17+
const regex = new RegExp(`^${path.replace(/\*/g, '.*')}$`)
18+
return regex.test(to.path)
19+
})
20+
if (!isIncluded) {
21+
return
22+
}
23+
}
24+
1425
// Do not redirect on login route, callback route and excluded routes
1526
const isExcluded = [...exclude, login, callback]?.some((path) => {
1627
const regex = new RegExp(`^${path.replace(/\*/g, '.*')}$`)

src/runtime/types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare module '@nuxt/schema' {
1717
export interface RedirectOptions {
1818
login: string
1919
callback: string
20+
include?: string[]
2021
exclude?: string[]
2122
cookieRedirect?: boolean
2223
}

0 commit comments

Comments
 (0)