From 3c4f7ef7a07830bf46b5f1d5a59541a4e54985a6 Mon Sep 17 00:00:00 2001 From: Zoey Kaiser Date: Tue, 23 Jan 2024 08:37:34 +0100 Subject: [PATCH] Updated middleware on server --- src/runtime/middleware/auth.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/runtime/middleware/auth.ts b/src/runtime/middleware/auth.ts index 8e6109df..b649ecfa 100644 --- a/src/runtime/middleware/auth.ts +++ b/src/runtime/middleware/auth.ts @@ -1,5 +1,5 @@ import { navigateToAuthPages, determineCallbackUrl } from '../utils/url' -import { navigateTo, defineNuxtRouteMiddleware, useRuntimeConfig, useAuth } from '#imports' +import { navigateTo, defineNuxtRouteMiddleware, useRuntimeConfig, useAuth, abortNavigation } from '#imports' type MiddlewareMeta = boolean | { /** Whether to only allow unauthenticated users to access this page. @@ -27,7 +27,7 @@ declare module '#app' { } } -export default defineNuxtRouteMiddleware((to) => { +export default defineNuxtRouteMiddleware(async (to) => { const metaAuth = typeof to.meta.auth === 'object' ? { unauthenticatedOnly: true, @@ -85,6 +85,18 @@ export default defineNuxtRouteMiddleware((to) => { } if (authConfig.provider.type === 'authjs') { + if (process.server) { + // @ts-ignore This is valid for fetching the providers, as we already know this middelware only runs for authjs + const { getProviders } = useAuth() + const providers = await getProviders() + const defaultProvider = providers[authConfig.provider.defaultProvider] ?? providers[Object.keys(providers)[0]] + if (defaultProvider) { + return navigateTo(defaultProvider.signinUrl, { external: true }) + } + // Fallback, if no provider signin url can be found, throw error page + return abortNavigation({ message: 'Please authenticate', statusCode: 403 }) + } + const signInOptions: Parameters[1] = { error: 'SessionRequired', callbackUrl: determineCallbackUrl(authConfig, () => to.fullPath) } // @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