Next12 中带有 Prisma 适配器的 NextAuth 凭据提供程序不执行任何操作
NextAuth Credential Provider with Prisma Adapter in Next12 does nothing
我已经使用 NextAuth CredentialsProvider 设置了我的 Nextjs (Next12) 并使用了 Prisma Adapter 将用户会话保存在数据库中。
我在此处从 NextAuth 团队那里关注了这个 documentation。但是在我点击登录按钮后没有任何反应。
注意事项
之前:-
- 我已经确保首先从数据库中获取数据并且它工作正常。
- 我也确实尝试过只使用普通的
session: { jwt: true, maxAge: 30 * 24 * 60 * 60 }
而不是直接使用 适配器 。也很好用。
问题
现在,我只想知道是否可以将 CredentialsProvider
与 Adapter
一起使用?
NextAuth API
以下是 2 个示例,或者一个有效,一个无效:/pages/api/auth/[...nextauth].js
- working: 不使用
adapter
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
}
}
})
}
- 不工作:使用
prisma adapter
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 文档本身):-
- 我已经完成了
npx prisma migrate dev
& npx prisma generate
// 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])
}
我已经使用 NextAuth CredentialsProvider 设置了我的 Nextjs (Next12) 并使用了 Prisma Adapter 将用户会话保存在数据库中。
我在此处从 NextAuth 团队那里关注了这个 documentation。但是在我点击登录按钮后没有任何反应。
注意事项
之前:-
- 我已经确保首先从数据库中获取数据并且它工作正常。
- 我也确实尝试过只使用普通的
session: { jwt: true, maxAge: 30 * 24 * 60 * 60 }
而不是直接使用 适配器 。也很好用。
问题
现在,我只想知道是否可以将 CredentialsProvider
与 Adapter
一起使用?
NextAuth API
以下是 2 个示例,或者一个有效,一个无效:/pages/api/auth/[...nextauth].js
- working: 不使用
adapter
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
}
}
})
}
- 不工作:使用
prisma adapter
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 文档本身):-
- 我已经完成了
npx prisma migrate dev
&npx prisma generate
// 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])
}