我现在就<代码>next 13和next-auth
开展工作。 我的错误是<代码>。 类型:植被 当我使用<条码>植被(req, res, authOptions)条码>验证用户时,标题不是条码>。
下面是我的文稿。
<>strong>users/routes.ts
import { NextRequest, NextResponse } from next/server
import { connectMongoDB } from "@/lib/mongodb";
import User from "@/models/user"
import { getServerSession } from next-auth ;
import { authOptions } from ../auth/[...nextauth]/route ;
export async function GET(req: NextRequest, res: NextResponse) {
const session = await getServerSession(req, res, authOptions)
if (!session) {
return NextResponse.json({ message: "You must be logged in.", status: 401 });
}
try {
await connectMongoDB();
const users = await User.find();
return NextResponse.json({ message: "", status: 200, data: users })
} catch (error) {
return NextResponse.json({ message: "Error occured..", status: 500 })
}
}
auth/[......nextauth]/routes.js
import { connectMongoDB } from "@/lib/mongodb";
import User from "@/models/user";
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
export const authOptions = {
providers: [
CredentialsProvider({
name: "Email and password",
credentials: {},
async authorize(credentials, req) {
const { email, password } = credentials;
try {
await connectMongoDB();
const user = await User.findOne({ email })
if (!user) {
return null;
}
const passwordMatch = await bcrypt.compare(password, user.password)
if (!passwordMatch) {
return null;
}
return user;
} catch (error) {
console.log( error , error)
return null;
}
},
}),
],
session: {
strategy: "jwt",
},
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: "/",
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
I have tried the getServerSession(authOptions)
and its worked but dont idea it is the correct way or not.
Please help me to understand/fix this issue with the correct way