💭
Next.jsのAPI RoutesでgetSessionを使うとnullがかえってくる
Next.jsのAPI RoutesでPOST時にNextAuth.jsのgetSession
を使うとnull
がかえってきてしまう
page/api/index.ts
import { NextApiRequest, NextApiResponse } from "next"
import { getSession } from 'next-auth/react'
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({ req })
// ・・・
}
error
[next-auth][error][CLIENT_FETCH_ERROR]
https://next-auth.js.org/errors#client_fetch_error undefined {
error: {},
url: 'http://localhost:3000/api/auth/session',
message: undefined
}
getSession
をgetServerSession
に書き換える
page/api/index.ts
import { NextApiRequest, NextApiResponse } from "next"
import { authOptions } from './auth/[...nextauth]'
import { getServerSession } from 'next-auth/next'
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const session = await getServerSession(req, res, authOptions)
// ・・・
}
ちゃんとかえってきます
参考
Discussion