From 38b4e0a1349beb22554c4cca5a69b6023964cb7c Mon Sep 17 00:00:00 2001 From: Louis Haftmann <30736553+LouisHaftmann@users.noreply.github.com> Date: Fri, 2 Jun 2023 17:17:54 +0200 Subject: [PATCH 1/8] feat: protected route redirect overwrite per page --- src/runtime/middleware/auth.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runtime/middleware/auth.ts b/src/runtime/middleware/auth.ts index 03ae93dd..fe3e652a 100644 --- a/src/runtime/middleware/auth.ts +++ b/src/runtime/middleware/auth.ts @@ -5,6 +5,7 @@ import { useAuth } from '#imports' type MiddlewareMeta = boolean | { unauthenticatedOnly: true, navigateAuthenticatedTo?: string, + navigateUnauthenticatedTo?: string } declare module '#app/../pages/runtime/composables' { @@ -61,6 +62,6 @@ export default defineNuxtRouteMiddleware((to) => { // @ts-ignore This is valid for a backend-type of `authjs`, where sign-in accepts a provider as a first argument return signIn(undefined, signInOptions) as ReturnType } else { - return navigateTo(authConfig.provider.pages.login) + return navigateTo(metaAuth.navigateUnauthenticatedTo ?? authConfig.provider.pages.login) } }) From 9c26a4ee4faf60b58c7346ad6a86c4d81648fc75 Mon Sep 17 00:00:00 2001 From: Louis Haftmann <30736553+LouisHaftmann@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:18:47 +0200 Subject: [PATCH 2/8] fix: don't access `navigateUnauthenticatedTo` if metaAuth is boolean --- src/runtime/middleware/auth.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/runtime/middleware/auth.ts b/src/runtime/middleware/auth.ts index fe3e652a..f0cd0aa1 100644 --- a/src/runtime/middleware/auth.ts +++ b/src/runtime/middleware/auth.ts @@ -62,6 +62,9 @@ export default defineNuxtRouteMiddleware((to) => { // @ts-ignore This is valid for a backend-type of `authjs`, where sign-in accepts a provider as a first argument return signIn(undefined, signInOptions) as ReturnType } else { - return navigateTo(metaAuth.navigateUnauthenticatedTo ?? authConfig.provider.pages.login) + if(typeof metaAuth === 'object' && metaAuth.navigateUnauthenticatedTo) + return navigateTo(metaAuth.navigateUnauthenticatedTo) + else + return navigateTo(authConfig.provider.pages.login) } }) From 77cdb48de73b49241bd8feb5237557daba2058af Mon Sep 17 00:00:00 2001 From: Louis Haftmann <30736553+LouisHaftmann@users.noreply.github.com> Date: Mon, 19 Jun 2023 08:35:05 +0200 Subject: [PATCH 3/8] docs: auth middleware options --- .../3.application-side/4.protecting-pages.md | 26 ++++++++++++++++++- src/runtime/middleware/auth.ts | 16 +++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/docs/content/3.application-side/4.protecting-pages.md b/docs/content/3.application-side/4.protecting-pages.md index 839f27c7..cb1aadba 100644 --- a/docs/content/3.application-side/4.protecting-pages.md +++ b/docs/content/3.application-side/4.protecting-pages.md @@ -1,11 +1,13 @@ # Protecting Pages `nuxt-auth` offers different approaches to protect pages: + 1. Global protection: Protects all pages with manual exceptions 2. Local protection: Protects specific pages 3. Custom middleware: Create your own middleware Briefly summarized, you can enable global protection (1) in your `nuxt.config.ts`: + ```ts export default defineNuxtConfig({ modules: ['@sidebase/nuxt-auth'], @@ -18,6 +20,7 @@ export default defineNuxtConfig({ Now *all pages* will require sign-in. Learn how to add excepted pages [below](/nuxt-auth/application-side/protecting-pages#disabling-the-global-middleware-locally) To enable page-local protection (2), add the following `definePageMeta` directive to a page: + ```vue