Next.js Discord

Discord Forum

Next-auth with app directory

Unanswered
Molossus of Epirus posted this in #help-forum
Open in Discord
Molossus of EpirusOP
I am not able to get the session object on the server. "getServerSession()" always returns null. I have seen that this is a common issue but have not found any solution to the problem. Any help is appreciated.

Code:

import { NextResponse } from 'next/server'
import { authOptions } from '../auth/[...nextauth]/route'
import { getServerSession } from 'next-auth/next'

export async function GET() {
  const session = await getServerSession(authOptions)
  
  if (!session) {
    console.log("No session found", session) // Always null
    return NextResponse.json({ success: false, error: 'Unauthorized' }) 
  }
  console.log("Session found")
  return NextResponse.json({ success: true })
}


.env.local:

NEXT_AUTH_SECRET=mysecret
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_URL_INTERNAL=http://127.0.0.1:3000

0 Replies