English 中文(简体)
类型:植被 主管并非下至下级的职能。
原标题:TypeError: res.getHeader is not a function in next-auth

我现在就<代码>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

问题回答

getServerSession(authOptions) 的工作完全罚款。

If you want to include the requests, you have to add headers to the getServerSession function along with authOptions.





相关问题
Code don t work, can t read property className of undefined

What is wrong with this code? var divarray = []; var articleHTML = []; var absHTML; var keyHTML; var bodyHTML = []; var i = 0; divarray = document.getElementById("yui-main").getElementsByTagName("div"...

Array subclasses cannot be deserialized, Error #1034

I ve just found a strange error when deserializing from a ByteArray, where Vectors cannot contain types that extend Array: there is a TypeError when they are deserialized. TypeError: Error #1034: ...

TypeError when trying to use observers and STI

I m trying to follow along with the thread on implementing an achievement system (located at How to implement an achievement system in RoR), and am running into a TypeError when the object is saved ...

Qt Python Calendar: selected day direct access

I have calendar that is working fine. Here is the function that display the full date: def selectDate(self,date): self.fullDate = str(date.day()) + " / " + str(date.month()) + " / " + str(date....

热门标签