Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth middleware is using "signIn" method on server side #30

Closed
timAdrien opened this issue Sep 29, 2023 · 1 comment
Closed

Auth middleware is using "signIn" method on server side #30

timAdrien opened this issue Sep 29, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@timAdrien
Copy link

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 timAdrien added the bug Something isn't working label Sep 29, 2023
@timAdrien
Copy link
Author

Wrong project, sorry, opened one on: sidebase/nuxt-auth#537

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant