nuxt-auth icon indicating copy to clipboard operation
nuxt-auth copied to clipboard

Unable to use getServerSession in server api route that starts with /api/auth

Open chriscdn opened this issue 2 years ago • 2 comments

Environment



Reproduction

No response

Describe the bug

I created a /server/api/ set of endpoints for connecting and disconnecting existing social accounts. I placed these files in a directory named /server/api/auth-connect. In these files, the getServerSession returns undefined since the api route (/api/auth-connect/) starts with the string /api/auth.

I traced the problem to the getServerSession method:

export const getServerSession = async (event) => {
  const authBasePath = useRuntimeConfig().public.auth.computed.pathname;
  if (event.path && event.path.startsWith(authBasePath)) {
    return null;
  }

  // .....
};

In my instance, authBasePath resolves to /api/auth. Since event.path resolves to /api/auth-connect/, the check passes and therefore returns null.

I can't comment on why this check is necessary, but doubt all routes that start with auth should be excluded from using this method.

Additional context

No response

Logs

No response

chriscdn avatar Sep 24 '23 06:09 chriscdn

Same here ... 2 days, I'm raging against the version trying to find something ! And here is @chriscdn finding the exact same problem !

In my case I changed the base url to baseURL: '/api/admin/auth' and got same problem.

The try catch gave me this information:

statusCode: 500, fatal: false, unhandled: false, statusMessage: 'Tried to get server session without setting up an endpoint to handle authentication (see https://github.com/sidebase/nuxt-auth#quick-start)', data: undefined }

adesousa avatar Sep 27 '23 20:09 adesousa

As a workaround I decided to change getServerSession by :

const authBasePath = useRuntimeConfig().public.auth.computed.pathname
const session = await $fetch(authBasePath + '/session', {
    method: 'GET',
    headers: event.headers
  })

It's working fine so far. 🚀

adesousa avatar Sep 27 '23 21:09 adesousa

Closed due to an old version. If anyone is still experiencing this issue, please check if it still exists in 0.8.0 and that the module has been setup using the new docs: https://auth.sidebase.io

zoey-kaiser avatar Jul 13 '24 10:07 zoey-kaiser