How to export HTTP methods?

I was using nextAuth with Pages router and i want to switch to Api router but i ‘m getting some error. I have created a testing project to make everything work before i change that in my real project. So in the test project, i have 2 pages: the homePage and the adminPage. In the homePage i have a login form and i’m using the credentials providers to connect, if it’s connected successfully, i want to be redirected to adminPage. I’m using mongodb atlas, [email protected] and [email protected].

I don’t have a problem with the db connection. The problem comes from the initialization of the […nextauth]/route.js file. I have used 2 method, the first Method is :

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import User from "@/app/user";

const handler = NextAuth({
  session: {
  strategy: "jwt",
},
providers: [
   CredentialsProvider({
      async authorize(credentials, req) {
      dbConnect();

      const { email, password } = credentials;

      console.log("We are here");

      const user = await User.findOne({ email }).select("+password");

      console.log("user from db");
      console.log(user);

      if (!user) {
        console.log("No user found");
        return "Invalid Email or Password";
      }
      console.log("User found");

      const isPasswordMatched = password === user?.password;

      if (!isPasswordMatched) {
        console.log("Password don't match");
        return "Invalid Email or Password";
      }
      console.log("Password match");

      return user;
    },
  }),
],
callbacks: {
  jwt: async ({ token, user }) => {
    user && (token.user = user);

    if (req.url === "/api/auth/session?update") {
      // hit the db and return the updated user

      const updatedUser = await User.findById(token.user._id);
      token.user = updatedUser;
    }

    return token;
  },
  session: async ({ session, token }) => {
    session.user = token.user;

    // delete password from session
    delete session?.user?.password;

    return session;
  },
},
pages: {
  signIn: "/",
},
  secret: process.env.NEXTAUTH_SECRET,
});

export { handler as GET, handler as POST };

So None of the console.log shown in this code is diplayed and it only says error with a status code 401. My login email and password are correct.

error shown in the terminal: “POST /api/auth/callback/credentials 401”

The second method is :

export default async function auth(req, res) {
 // Do whatever you want here, before the request is passed down to `NextAuth`
 return await NextAuth(req, res, {
   session: {
     strategy: "jwt",
   },
   providers: [
     CredentialsProvider({
       async authorize(credentials, req) {
         dbConnect();

         const { email, password } = credentials;

         const user = await User.findOne({ email }).select("+password");

         if (!user) {
           return "Invalid Email or Password";
         }

         const isPasswordMatched = password === user?.password

         if (!isPasswordMatched) {
           return "Invalid Email or Password";
         }

         return user;
       },
     }),
   ],
   callbacks: {
     jwt: async ({ token, user }) => {
       user && (token.user = user);

       if (req.url === "/api/auth/session?update") {
         // hit the db and return the updated user

         const updatedUser = await User.findById(token.user._id);
         token.user = updatedUser;
       }

       return token;
     },
     session: async ({ session, token }) => {
       session.user = token.user;

       // delete password from session
       delete session?.user?.password;

       return session;
     },
   },
   pages: {
     signIn: "/",
   },
   secret: process.env.NEXTAUTH_SECRET,
 });
}

The error of this method is “Not exporting HTTP Method”, not any other clarifications. I have checked the document of nextAuth and i didn’t find anything that could help me.

Here is a screen caption of the terminal:

I want to use the second method for my project but i wouldn’t mind if you can help correct both of the 2 ways

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật