I am using nextAuth as the authentication management and I am able to get data response via useSession() hook but when I tried to do this for getServerSession() I am getting null on the page. But in the network tab I am able to see the data of session.
This is my /app/api/auth/[…nextauth]/route.ts
import NextAuth from "next-auth";
import CredentialsProvider from 'next-auth/providers/credentials';
export const handler = NextAuth({
providers:[
CredentialsProvider({
name: "Email",
credentials:{
username: {label: "Username", type: "text", placeholder: "Username"},
password: {label: "Password", type: "password", placeholder: "Password"}
},
async authorize(credentials: any){
console.log(credentials)
return {
id: "user1",
name: "Jitesh Rajoriya",
email: "[email protected]"
}
}
})
],
secret: process.env.NEXTAUTH_SECRET
})
export const GET = handler;
export const POST = handler;
and this is my /app/user/page.tsx
import { getServerSession } from "next-auth/next";
import { handler } from "../api/auth/[...nextauth]/route";
async function getUser() {
const session = await getServerSession(handler);
return session;
}
export default async function(){
const session = await getUser();
return <div>
{JSON.stringify(session)}
</div>
}
This is my .env file
NEXTAUTH_URL = http://localhost:3000 NEXTAUTH_SECRET = mysecretpassword_nextauth
and I am getting this
enter image description here In this image you can see that the data is available in the response but not on the page.
I am expecting that the problem might be in the component that how I am rendering it. Or maybe problem in the version of nextAuth and NextJS
Jitesh Rajoriya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.