getSession({req}) not working in api route
See original GitHub issueQuestion 💬
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:
- Created a year ago
- Comments:10 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?
The initial description/issue title did not mention
getServerSideProps
.A reproduction repository would be very heplful.