nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 925 forks source link

redirect cause `Cannot set headers after they are sent` error #802

Closed PlusA2M closed 3 years ago

PlusA2M commented 4 years ago

Version

v5.0 (latest development build) v4.9.1

Reproduction link

https://github.com/nuxt-community/auth-module/blob/dev/src/core/middleware.ts#L20:L24

Steps to reproduce

1) Set any page with auth: 'guest' 2) Error on server-sides Cannot set headers after they are sent to the client

What is expected ?

No error

What is actually happening?

Error thrown

Additional comments?

I'm currently using @nuxtjs/auth-next, the code on https://github.com/nuxt-community/auth-module/blob/dev/src/core/middleware.ts#L20:L24 should redirect the user to the correct page, however, I think that's a chance for my server to run code on L26:27 const { tokenExpired, refreshTokenExpired, isRefreshable } = ctx.$auth.check(true) before the redirect is finished, therefore causing the error.

This bug report is available on Nuxt community (#c645)
nikolayandreev commented 4 years ago

I see it too, using Laravel Sanctum like this:

auth: {
    strategies: {
      cookie: {
        cookie: {
          name: 'XSRF-TOKEN',
        },
      },
      laravelSanctum: {
        provider: 'laravel/sanctum',
        url: API_BASE_URL,
        endpoints: {
          login: { url: '/api/login' },
          logout: { url: '/api/logout', method: 'DELETE' },
          user: false,
        },
      },
      autoFetchUser: false,
      globalToken: false,
      tokenRequired: false,
      tokenType: false,
    },
    redirect: {
      login: '/login',
      logout: '/',
      callback: '/login',
      home: '/dashboard',
    },
  },

I've defined the auth middleware in nuxt.config.js as router middleware

router: {
    middleware: ['auth'],
  },

The pages are as follows:

dashboard --index login.vue - auth: 'guest' register.vue - auth: 'guest' index.vue - auth: 'guest'

If I try to put middlewares on the layouts it stops working..

@pi0 Just wanted to notify you about this issue, since @nuxtjs/auth v5 is under development it could hopefully be debugged in the next version.

SebastianMieszczanczyk commented 4 years ago

"@nuxtjs/auth": "^4.9.1" I see it also in older version. Error below:

ERROR Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:526:11) at Storage.setCookie (server.js:1410:20) at Storage.setUniversal (server.js:1222:10) at Storage.syncUniversal (server.js:1252:12) at Auth.syncToken (server.js:555:26) at CustomScheme.syncTokens (server.js:1041:22) at refreshAuthToken (server.js:36332:20) at server.js:36380:5 at server.js:1587:45 at processTicksAndRejections (internal/process/task_queues.js:97:5)

misaon commented 3 years ago

+1

Today I reproduce the same mistake. I followed the Nuxt.js documentation and an error popped up.

For me, these are two problems.

  1. If I go to the /login page and I am already logged in, an error will pop up (see screen) and redirect to "home".
  2. If I want to log in and the data from "/user" is returned from the response, there will be no redirect to "home" but it will remain on the "/login" page. After manual reload, it is redirected.

image image image

JoaoPedroAS51 commented 3 years ago

Thank you for report! We'll take a look at this issue :)

nathanchase commented 2 years ago

This is still a problem, FYI. Always happens when redirecting from login to homepage.