I’m using NextJS 12 with Auth0 but when I try to access the session from an api/route is always null.
import { getSession } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = getSession(req, res);
const user = session?.user;
console.log("SESSION: ", { session }); // null
res.json({ protected: "My Secret", id: user?.sub });
}
But when using getSession() from the page or getServerSideProps the session is returned corretly.
Is there something I’m missing?
Could this be because you didn’t wrap the handler around withApiAuthRequired?
I’m also seeing this and I have the withApiAuthRequired
export default withApiAuthRequired(async function (req: any, res: any) {
const { method } = req;
const session = getSession(req, res);
const user = session?.user;
console.log(session.user)
...
}
});
Since getSession() returns Promise<default | null | undefined> you need to wait for the promise to resolve or reject.
const session = await getSession(req, res)
1 Like
system
Closed
5
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.