Next12 中带有 Prisma 适配器的 NextAuth 凭据提供程序不执行任何操作

NextAuth Credential Provider with Prisma Adapter in Next12 does nothing

我已经使用 NextAuth CredentialsProvider 设置了我的 Nextjs (Next12) 并使用了 Prisma Adapter 将用户会话保存在数据库中。

我在此处从 NextAuth 团队那里关注了这个 documentation。但是在我点击登录按钮后没有任何反应

注意事项

之前:-

问题

现在,我只想知道是否可以将 CredentialsProviderAdapter 一起使用?

NextAuth API

以下是 2 个示例,或者一个有效,一个无效:/pages/api/auth/[...nextauth].js

import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';

export default async function auth(req, res) {
  return await NextAuth(req, res, {
    secret: process.env.SECRET,
    adapter: PrismaAdapter(prisma),
    session: {
      jwt: true,
      maxAge: 30 * 24 * 60 * 60, // 30 days
    }
    providers: [
      CredentialsProvider({
        async authorize(credentials) {
          const user = await prisma.user.findFirst({
            where: {
                email: credentials.email,
                password: credentials.password
            }
          });

          if (user !== null)
          {
              return user;
          }
          else {
            throw new Error('User does not exists. Please make sure you insert the correct email & password.')
          }
        }
      })
    ],
    callbacks: {
      redirect: async ({ url, baseUrl }) => {
        return baseUrl
      },
      jwt: async ({ token, user, account, profile, isNewUser }) => {
        if (typeof user !== typeof undefined) token.user = user;
  
        return token
      },
      session: async ({ session, user, token }) => {
        token?.user && (session.user = token.user)
  
        return session
      }
    }
  })
}
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { PrismaClient } from '@prisma/client';
import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
const prisma = new PrismaClient()

export default async function auth(req, res) {
  return await NextAuth(req, res, {
    secret: process.env.SECRET,
    adapter: PrismaAdapter(prisma),
    providers: [
      CredentialsProvider({
        async authorize(credentials) {
          const user = await prisma.user.findFirst({
            where: {
                email: credentials.email,
                password: credentials.password
            }
          });

          if (user !== null)
          {
              return user;
          }
          else {
            throw new Error('User does not exists. Please make sure you insert the correct email & password.')
          }
        }
      })
    ],
    callbacks: {
      redirect: async ({ url, baseUrl }) => {
        return baseUrl
      },
      jwt: async ({ token, user, account, profile, isNewUser }) => {
        if (typeof user !== typeof undefined) token.user = user;
  
        return token
      },
      session: async ({ session, user, token }) => {
        token?.user && (session.user = token.user)
  
        return session
      }
    }
  })
}

Prisma 模式

这是当前的 schema.prisma(这来自 NextAuth 文档本身):-

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["referentialIntegrity"]
}

datasource db {
  provider             = "mysql"
  url                  = env("DATABASE_URL")
  // shadowDatabaseUrl    = env("SHADOW_URL")
  referentialIntegrity = "prisma"
}

model Account {
  id                 String  @id @default(cuid())
  userId             String
  type               String
  provider           String
  providerAccountId  String
  refresh_token      String?
  access_token       String?
  expires_at         Int?
  token_type         String?
  scope              String?
  id_token           String?
  session_state      String?
  oauth_token_secret String?
  oauth_token        String?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  password      String?
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

我刚刚发现 next-auth 中的 CredentialsProvider 根据 doc 根本无法与 adapter 一起使用。您可能会发现它不能或不推荐与 CredentialsProvider.

一起使用的原因

这是一个额外的link,您可以在其中找到有关此事的讨论。