when i go to chats page The page is not responding due to this error, and when I open the console, I find the number of errors increasing non-stop, and this sentence appears to me:
Uncaught (in promise) Error:
headers was called outside a request scope.
Read more:
text
chats.tsx page code :
"use client";
import { auth } from "@/auth";
import { getUsersForSidebar } from "@/lib/data";
import Chat from "./chat";
import { useState } from "react";
import { IChatDocument } from "@/models/chatModel";
const Chats = async () => {
const [selectedChat, setSelectedChat] = useState<IChatDocument | null>(null);
const session = await auth();
const chats = session?.user ? await getUsersForSidebar(session.user._id) : [];
const handleSelectedChat = (chat: IChatDocument) => setSelectedChat(chat);
return (
<nav>
<ul>
{chats.map((chat) => (
<Chat
key={chat._id}
chat={chat}
handleSelectedChat={handleSelectedChat}
selectedChat={selectedChat}
/>
))}
</ul>
</nav>
);
};
export default Chats;
The application is supposed to show the users I have chatted with, and when I click on a user, it takes me to the chat page with him, but the application does not respond as soon as I reach the page where the chat list appears.
auth.ts that i using it in chats.tsx
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
GitHub({
clientId: process.env....etc,
clientSecret: process.env....etc,
}),
],
secret: process.env....etc,
callbacks: {
async session({ session }) {
try {
await connectToMongoDB();
if (session.user) {
const user = await User.findOne({ email: session.user.email });
if (user) {
session.user._id = user._id;
return session;
} else {
throw new Error("error");
}
} else {
throw new Error("Invalid Session!");
}
} catch (error) {
throw new Error("Invalid session");
}
},
async signIn({ account, profile }) {
if (account?.provider === "github") {
await connectToMongoDB();
try {
const user = await User.findOne({ email: profile?.email });
if (!user) {
const newUser = await User.create({
username: profile?.login,
email: profile?.email,
avatar: profile?.avatar_url,
});
await newUser.save();
}
return true; // indicate successful sign-in
} catch (error) {
catcing error...
return false;
}
}
return false;
},
},
});
any help i’ll be thankful