Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[Question]: getSession() in API returns null even when session exists #2302

Closed
aspctu opened this issue Jul 3, 2021 · 0 comments
Closed
Labels
question Ask how to do something or how something works

Comments

@aspctu
Copy link

aspctu commented Jul 3, 2021

Question 💬

In short, when securing an API route with getSession, any time a page attempts to fetch from the API, getSession returns null but if I go to the API link directly, it works as expected.

To test this, I have a test API route at the path pages/api/test.js that calls getSession({ req }) and returns True if a session is found, False if it isn't.

import { getSession } from 'next-auth/client'

export default async (req, res) => {
  const session = await getSession({ req })
  if (session) {
    // Signed in
    res.json({"valid" : true})
  } else {
    // Not Signed in
    res.json({"valid": false})
  }
  res.end()
}

I also have a basic Page component at pages/test/index.js. Via getServerSideProps, I fetch a response from the API and pass the response as a prop to the Page component. Every time, regardless if I'm signed in or not, the response from the test route is {valid: false}.

However, if I point my browser to localhost:3000/api/test, I receive the expected response. Any help here would be appreciated! I'm using Google Sign In with my own MySQL database.

How to reproduce ☕️

No response

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

@aspctu aspctu added the question Ask how to do something or how something works label Jul 3, 2021
@aspctu aspctu changed the title [Bug]: getSession() in API returns null even when session exists [Question]: getSession() in API returns null even when session exists Jul 3, 2021
@nextauthjs nextauthjs locked and limited conversation to collaborators Jul 3, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests

2 participants