prisma / blogr-nextjs-prisma

111 stars 83 forks source link

Next v13 support? #26

Closed f00d4tehg0dz closed 1 year ago

f00d4tehg0dz commented 1 year ago

After upgrading the final branch to Next.js 13 and Next Auth 4.22.x you are no longer able to make an authenticated post. Sign-in authentication works as expected, however.

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
}

Invalid prisma.post.create() invocation:

{
  data: {
    title: "Test",
    content: "test content",
    author: {
      connect: {
        email: undefined,
?       id?: String
      }
    }
  }
}
GeneBuchite commented 1 year ago

I fixed this problem by copy/paste the [...nextauth].ts from (https://github.com/nextauthjs/next-auth-example/tree/main/pages/api/auth)

Then in the // pages/api/post/index.ts. Add: import { getServerSession } from "next-auth"; import { authOptions } from "../auth/[...nextauth]"

Remove: import { getSession } from 'next-auth/react';

change: const session = await getSession({ req }); to: const session = await getServerSession(req, res, authOptions);

Hope this helps!

jessycormier commented 7 months ago

const session = await getServerSession(req, res, authOptions);

This was key for me getting things moving again; Thanks!