getServerSession in next-auth not working
Unanswered
i_lost_to_loba_kreygasm posted this in #help-forum
2 Replies
import db from '../../../utils/mongo';
import { getServerSession } from 'next-auth/react';
import { authOptions } from './auth/[...nextauth]';
export default async function handler(req, res) {
const session = await getServerSession(req, res, authOptions);
console.log(session)
if(session){
let foundEmail=await db.collection("users").findOne({email:session?.user?.email});
if(foundEmail){
res.json({msg:'email already exists'})
}
else{
await db.collection("users").insertOne({email:session.user.email,posts:[]});
res.json({msg:'Email added'})
}
}
res.json({msg:'hello'})
}
here is the full codeGiant panda
correct this line
const session = await getServerSession(req, res, authOptions);
to
const session = await getServerSession(authOptions);
const session = await getServerSession(req, res, authOptions);
to
const session = await getServerSession(authOptions);