NuxtN
Nuxt16mo ago
4 replies
Taylan

getServerSession() returns null on server middleware

Hi,

import { getServerSession, getToken } from '#auth';

export default eventHandler(async (event) => {
  const session = await getServerSession(event);
      if (!session) {
          throw createError({
              statusMessage: 'Unauthenticated',
              statusCode: 403
          });
      }
});


inside of server/middleware/auth.ts returns null on same cases. There was a warning about it and recommended solution was to pass cookies to headers and i have already applied it during any request. But still, especially on page refresh, session returns null.

Also, i realized that if there is a async fetch call inside of any .vue component's onMounted() method, this middleware is called. Ex: if there are 4 calls it is called 4 times even if i dont mount that page.

Any recommendations to overcome this issue?
Was this page helpful?