|
1 | | -import { determineCallbackUrl } from '../utils/url' |
| 1 | +import { determineCallbackUrl, isExternalUrl } from '../utils/url' |
2 | 2 | import { isProduction } from '../helpers' |
| 3 | +import { ERROR_PREFIX } from '../utils/logger' |
3 | 4 | import { defineNuxtRouteMiddleware, navigateTo, useAuth, useRuntimeConfig } from '#imports' |
4 | 5 |
|
5 | 6 | type MiddlewareMeta = boolean | { |
@@ -98,23 +99,30 @@ export default defineNuxtRouteMiddleware((to) => { |
98 | 99 | return navigateTo(options.navigateUnauthenticatedTo) |
99 | 100 | } |
100 | 101 |
|
| 102 | + const loginPage = authConfig.provider.pages.login |
| 103 | + if (typeof loginPage !== 'string') { |
| 104 | + console.warn(`${ERROR_PREFIX} provider.pages.login is misconfigured`) |
| 105 | + return |
| 106 | + } |
| 107 | + |
101 | 108 | // Default callback URL was provided |
| 109 | + const external = isExternalUrl(loginPage) |
102 | 110 | if (typeof globalAppMiddleware === 'object' && globalAppMiddleware.addDefaultCallbackUrl) { |
103 | 111 | let redirectUrl: string = to.fullPath |
104 | 112 | if (typeof globalAppMiddleware.addDefaultCallbackUrl === 'string') { |
105 | 113 | redirectUrl = globalAppMiddleware.addDefaultCallbackUrl |
106 | 114 | } |
107 | 115 |
|
108 | 116 | return navigateTo({ |
109 | | - path: authConfig.provider.pages.login, |
| 117 | + path: loginPage, |
110 | 118 | query: { |
111 | 119 | redirect: redirectUrl |
112 | 120 | } |
113 | | - }) |
| 121 | + }, { external }) |
114 | 122 | } |
115 | 123 |
|
116 | 124 | // Fall back to login page |
117 | | - return navigateTo(authConfig.provider.pages.login) |
| 125 | + return navigateTo(loginPage, { external }) |
118 | 126 | }) |
119 | 127 |
|
120 | 128 | interface MiddlewareOptionsNormalized { |
@@ -146,7 +154,7 @@ function normalizeUserOptions(userOptions: MiddlewareMeta | undefined): Middlewa |
146 | 154 | if (userOptions.unauthenticatedOnly === undefined) { |
147 | 155 | if (!isProduction) { |
148 | 156 | console.warn( |
149 | | - '[@sidebase/nuxt-auth] `unauthenticatedOnly` was not provided to `definePageMeta` - defaulting to Guest Mode enabled. ' |
| 157 | + `${ERROR_PREFIX} \`unauthenticatedOnly\` was not provided to \`definePageMeta\` - defaulting to Guest Mode enabled. ` |
150 | 158 | + 'Read more at https://auth.sidebase.io/guide/application-side/protecting-pages#middleware-options' |
151 | 159 | ) |
152 | 160 | } |
|
0 commit comments