diff --git a/src/runtime/composables/local/useAuth.ts b/src/runtime/composables/local/useAuth.ts index 1368fe0d..cd4ff21c 100644 --- a/src/runtime/composables/local/useAuth.ts +++ b/src/runtime/composables/local/useAuth.ts @@ -10,7 +10,7 @@ import { type UseAuthStateReturn, useAuthState } from './useAuthState' import { callWithNuxt } from '#app/nuxt' // @ts-expect-error - #auth not defined import type { SessionData } from '#auth' -import { navigateTo, nextTick, useNuxtApp, useRuntimeConfig } from '#imports' +import { navigateTo, nextTick, useNuxtApp, useRoute, useRuntimeConfig } from '#imports' type Credentials = { username?: string, email?: string, password?: string } & Record @@ -60,7 +60,13 @@ const signIn: SignInFunc = async (credentials, signInOptions, const { redirect = true, external } = signInOptions ?? {} let { callbackUrl } = signInOptions ?? {} if (typeof callbackUrl === 'undefined') { - callbackUrl = await determineCallbackUrl(runtimeConfig.public.auth, () => getRequestURLWN(nuxt)) + const redirectQueryParam = useRoute()?.query?.redirect + if (redirectQueryParam) { + callbackUrl = redirectQueryParam.toString() + } + else { + callbackUrl = await determineCallbackUrl(runtimeConfig.public.auth, () => getRequestURLWN(nuxt)) + } } if (redirect) { return navigateTo(callbackUrl, { external }) diff --git a/src/runtime/middleware/auth.ts b/src/runtime/middleware/auth.ts index 4f32140d..333d35aa 100644 --- a/src/runtime/middleware/auth.ts +++ b/src/runtime/middleware/auth.ts @@ -105,6 +105,19 @@ export default defineNuxtRouteMiddleware((to) => { return navigateTo(metaAuth.navigateUnauthenticatedTo) } else { + if (typeof globalAppMiddleware === 'object' && globalAppMiddleware.addDefaultCallbackUrl) { + let redirectUrl: string = to.fullPath + if (typeof globalAppMiddleware.addDefaultCallbackUrl === 'string') { + redirectUrl = globalAppMiddleware.addDefaultCallbackUrl + } + + return navigateTo({ + path: authConfig.provider.pages.login, + query: { + redirect: redirectUrl + } + }) + } return navigateTo(authConfig.provider.pages.login) } })