-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
getSession() returns null because request body is sent to next-auth url #7167
Comments
Using |
@clueleaf I don't think getServerSession is adequate. I tried that in an API endpoint and it returns the following error. Further, this solution will require everyone to refactor their existing codebases to replace getSession. I think this is a real bug that should be fixed. Please reopen, thanks! |
@therecluse26 Are you passing In // /pages/path/index.tsx
import { GetServerSideProps } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../api/auth/[...nextauth]";
export const getServerSideProps: GetServerSideProps = async (context) => {
const session = await getServerSession(context.req, context.res, authOptions);
// ...
} In API route: // /pages/api/path/index.ts
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
const session = await getServerSession(req, res, authOptions);
// ...
} Also see: https://next-auth.js.org/tutorials/securing-pages-and-api-routes#server-side |
If you are not able to get session use gettoken instead
Keys and URL
this is api route for calling token
and the in your layout/page
|
create file: ' .eslintrc.json ' in the root of the application with the following contents:
|
Environment
System:
OS: macOS 13.3
CPU: (10) arm64 Apple M1 Pro
Memory: 1.49 GB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.14.1 - /usr/local/bin/node
Yarn: 1.22.18 - /opt/homebrew/bin/yarn
npm: 9.3.1 - /usr/local/bin/npm
Browsers:
Chrome: 111.0.5563.146
Firefox: 111.0.1
Safari: 16.4
Reproduction URL
none
Describe the issue
After updated to v4.21.1,
await getSession({ req })
started to returnnull
for POST API requests, with following logs.It appeared that this change is causing the issue when body is added to the request to next-auth api route. (When I manually removed these lines, the problem got solved.)
next-auth/packages/next-auth/src/client/_utils.ts
Lines 47 to 50 in d69f311
How to reproduce
Here's my
[...nextauth].ts
(only essential parts):Expected behavior
getSession
returns session instead ofnull
;The text was updated successfully, but these errors were encountered: