question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getSession({req}) not working in api route

See original GitHub issue

Question 💬

Hello!

I have an api route which I am trying to protect by getting the session using const session = await getSession({req}).

However, it always returns null; Any help diagnosing the issue here is much appreciated, I have looked through the example app and I really can’t see what I am doing differently that would cause this.

How to reproduce ☕️

My api route is as follows:

import { getSession } from "next-auth/react";

const { CosmosClient } = require("@azure/cosmos");

export default async (req, res)  => {
    const session = await getSession({req});
    console.log(session); //null
}

[…nextauth].js

import NextAuth from "next-auth"
import GithubProvider from "next-auth/providers/github"

export default NextAuth({
  providers: [
    GithubProvider({
      clientId: process.env.GITHUB_ID,
      clientSecret: process.env.GITHUB_SECRET,
    })
  ],
})

_app.js

import { SessionProvider } from "next-auth/react"
import { useSession, signIn, signOut } from "next-auth/react"

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (
    <SessionProvider session={session}>
      <User></User>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

function User() {
  const { data: session } = useSession()
  if (session) { 
    return (
      <>        
        Signed in as {session.user.email} <br />        
        <button onClick={() => signOut()}>Sign out</button>      
      </>
    )
  } 
  return (
    <>      
      Not signed in <br />      
      <button onClick={() => signIn()}>Sign in</button>    
    </>
  )
}

It is worth noting that the sign in / sign out works correctly and shows my email when I am logged in at the same time that getSession() in my api route returns null

Contributing 🙌🏽

No, I am afraid I cannot help regarding this

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Klin-Coderscommented, May 25, 2022

firstly its is not advised to call your api routes using getServerSideProps…

but do you mind showing how you are using it in getServerSideProps?? to see if i can help maybe?

0reactions
balazsorban44commented, May 31, 2022

The initial description/issue title did not mention getServerSideProps.

A reproduction repository would be very heplful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getSession({req}) not working in api route #4647 - GitHub
I have an api route which I am trying to protect by getting the session using const session = await getSession({req}) .
Read more >
Next Auth getSession not working in api routes - Stack Overflow
when I call getSession in getServerSideProps() I get a valid object. export async function getServerSideProps({ req }) { const session = await ...
Read more >
Securing pages and API routes - NextAuth.js
The methods getSession() and getToken() both return an object if a session is valid and null if a session is invalid or has...
Read more >
Using the getSession function | SuperTokens Docs
Sometimes, you want an API to be accessible even if there is no session. In that case, you can use the sessionRequired flag:...
Read more >
next-session - npm
Usage in API Routes may result in API resolved without sending a response . This can be solved by either adding: import nextSession...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found