Next.js Discord

Discord Forum

getServerSession in next-auth not working

Unanswered
Siberian posted this in #help-forum
Open in Discord
Avatar
SiberianOP
Someone please help me !
Image

2 Replies

Avatar
SiberianOP
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 code
Avatar
Giant panda
correct this line
const session = await getServerSession(req, res, authOptions);
to
const session = await getServerSession(authOptions);