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

User is null in nextjs api route handler #802

Open
1 of 2 tasks
muezz opened this issue Jun 20, 2024 · 5 comments
Open
1 of 2 tasks

User is null in nextjs api route handler #802

muezz opened this issue Jun 20, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@muezz
Copy link

muezz commented Jun 20, 2024

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I have set up auth with google in my project and it is working fine. I have set up the middleware and all the createClient functions for server and client. I have the correct auth state in both the server and the client.

But in API route handlers, I get user as null even though I am logged in.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Start a new Nextjs project.
  2. Follow the docs to implement auth with the ssr package.
  3. Sign in
  4. Call an API endpoint where you call getSession()

Expected behavior

I should get the session info of the current user.

System information

  • OS: macOS
  • Browser: Arc
  • Version of supabase-js: 2.43.5
  • Version of supabase/ssr: 0.3.0
  • Version of Node.js: 20.12.2

Additional context

I think the problem is happening because cookies are not being passed to the route handler. But I think they should be passed automatically through the middleware. Why is that not happening?

@muezz muezz added the bug Something isn't working label Jun 20, 2024
@j4w8n
Copy link

j4w8n commented Jun 20, 2024

@muezz
Copy link
Author

muezz commented Jun 20, 2024

@j4w8n Hmmm. It does make sense logically but there should be an easier way to do this instead of manually adding the cookies on each call. Can the middleware not do this?

@j4w8n
Copy link

j4w8n commented Jun 20, 2024

@j4w8n Hmmm. It does make sense logically but there should be an easier way to do this instead of manually adding the cookies on each call. Can the middleware not do this?

I'm not familiar with Next, I just recall someone having a similar issue in the Discord community.

@muezz
Copy link
Author

muezz commented Jun 20, 2024

@j4w8n I will keep this open in the hope that there is an easier solution since the blog post is almost a year old.

@azharhussain96
Copy link

Can confirm that adding cookie property manually to the fetch in the server component does fix the issue. Unsure if there is something about my Next/Supabase version that is interfering with the Supabase-provided middleware boilerplate as it doesn't seem to be a ubiquitous problem.

  const response = await fetch(`${baseUrl}/api/organization`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'cookie': headers().get('cookie') || ''
    },
    body: JSON.stringify(orgData),
  });

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

3 participants