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

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

Closed
chriscdn opened this issue Sep 24, 2023 · 3 comments
Closed
Labels
bug A bug that needs to be resolved p3 Minor issue

Comments

@chriscdn
Copy link

Environment


  • Operating System: Darwin
  • Node Version: v16.20.0
  • Nuxt Version: 3.6.5
  • Nitro Version: 2.6.3
  • Package Manager: yarn@1.22.19
  • Builder: vite
  • User Config: sourcemap, app, components, css, build, vite, modules, auth, proxy, runtimeConfig
  • Runtime Modules: @nuxt/image-edge@1.0.0-rc.2-28257829.3628546, @sidebase/nuxt-auth@0.6.0-beta.5, @nuxt-alt/proxy@2.3.2
  • Build Modules: -

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 chriscdn added the bug label Sep 24, 2023
@adesousa
Copy link

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
Copy link

adesousa commented Sep 27, 2023

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. 🚀

@zoey-kaiser zoey-kaiser added p3 Minor issue bug A bug that needs to be resolved and removed bug labels Feb 23, 2024
@zoey-kaiser
Copy link
Member

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 zoey-kaiser closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug that needs to be resolved p3 Minor issue
Projects
None yet
Development

No branches or pull requests

3 participants