Auth middleware is using "signIn" method on server side
Closed this issue · 1 comments
timAdrien commented
Environment
No response
Reproduction
No response
Describe the bug
I'm using:
auth: {
provider: {
type: 'authjs'
},
globalAppMiddleware: true
}
in nuxt.config.ts
Which means that it enables the default auth middleware:
import { navigateTo, defineNuxtRouteMiddleware, useRuntimeConfig } from "#app";
import { determineCallbackUrl } from "../utils/url.mjs";
import { useAuth } from "#imports";
export default defineNuxtRouteMiddleware((to) => {
const metaAuth = typeof to.meta.auth === "object" ? {
unauthenticatedOnly: true,
...to.meta.auth
} : to.meta.auth;
if (metaAuth === false) {
return;
}
const authConfig = useRuntimeConfig().public.auth;
const { status, signIn } = useAuth();
const isGuestMode = typeof metaAuth === "object" && metaAuth.unauthenticatedOnly;
if (isGuestMode && status.value === "unauthenticated") {
return;
}
if (typeof metaAuth === "object" && !metaAuth.unauthenticatedOnly) {
return;
}
if (status.value === "authenticated") {
if (isGuestMode) {
return navigateTo(metaAuth.navigateAuthenticatedTo ?? "/");
}
return;
}
if (authConfig.globalAppMiddleware.allow404WithoutAuth) {
const matchedRoute = to.matched.length > 0;
if (!matchedRoute) {
return;
}
}
if (authConfig.provider.type === "authjs") {
const signInOptions = { error: "SessionRequired", callbackUrl: determineCallbackUrl(authConfig, () => to.path) };
return signIn(void 0, signInOptions);
} else if (typeof metaAuth === "object" && metaAuth.navigateUnauthenticatedTo) {
return navigateTo(metaAuth.navigateUnauthenticatedTo);
} else {
return navigateTo(authConfig.provider.pages.login);
}
});
But as you can see there is no "process.server" check before running "signIn" method. And as shown in documentation: https://next-auth.js.org/getting-started/client#signin
this method is client side only.
This causes this error: [nuxt] [request error] [unhandled] [500] Cannot set headers after they are sent to the client
Additional context
No response
Logs
No response
timAdrien commented
Wrong project, sorry, opened one on: sidebase/nuxt-auth#537