嗨,我试图实现一个登录与下一个13有appDir: true
和下一个的版本是13.3.0,但我检查了文档,但事实是我不明白,我这样做与 typescript ,但几乎没有文档,我不知道如果我这样做是正确的。
app/API/auth/[...nextauth]/route.ts
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
const handler = NextAuth({
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
const { username, password } = credentials as { username: string; password: string };
const res = await fetch("http://localhost:3000/api/auth/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username, password }),
});
const user = await res.json();
if (res.ok) {
return user;
} else {
return null;
}
},
}),
],
session: {
strategy: "jwt"
},
});
export { handler as GET, handler as POST };
layout.tsx
'use client'
import './globals.css'
import { SessionProvider } from "next-auth/react"
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>
<SessionProvider>
{children}
</SessionProvider>
</body>
</html>
)
}
当我添加<SessionProvider>{children}</SessionProvider>
时,我得到以下错误,它不加载页面。x1c 0d1x
2条答案
按热度按时间ix0qys7i1#
第一个
看起来仍然是under development。
你可以关注这个仓库的最新变化。一旦有一个工作的例子,我会更新这个答案。
秒
可能是您的
await fetch(...)
调用返回了一个HTTP 500 Internal Server Error
代码,并返回了一个错误页面,这是一个无效的JSON文档。添加
console.log(await res.text())
而不是const user = await res.json()
来验证这一点。qcbq4gxm2#
app/API/auth/[... nextauth]/route.ts
app/Providers.tsx
layout.tsx
并且当发送凭证时,发送相同的凭证和数据。