TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest' | property 'socket' -> object with constructor 'TLSSocket' --- property '_httpMessage' closes the circle

import Moralis from 'moralis';

const config = {
    domain: process.env.APP_DOMAIN,
    statement: 'Please sign this message to confirm your identity.',
    uri: process.env.NEXTAUTH_URL,
    timeout: 60,
};

export default async function handler(req, res) {
    const { address, chain, network } = req.body;

    await Moralis.start({ apiKey: process.env.MORALIS_API_KEY });

    try {
        const message = await Moralis.Auth.requestMessage({
            address,
            chain,
            network,
            ...config,
        });

        res.status(200).json(message);
    } catch (error) {
        res.status(400).json({ error });
        console.error(error);
    }
}

this is my code I have copied everything from the tutorial perfectly but I still get this error whenever I call it
I believe the error is coming from the request message

this is the error I am getting

error - TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'TLSSocket'
    --- property '_httpMessage' closes the circle
Unhandled Runtime Error
Error: Request failed with status code 500

Can you try to add some logging for the parameters to see if everything looks as expected?

this is the line that give that error?


    await Moralis.start({ apiKey: process.env.MORALIS_API_KEY });
    console.log('Moralis started');

    try {
        console.log('Trying to request message');
        const message = await Moralis.Auth.requestMessage({
            address,
            chain,
            network,
            ...config,
        });
        console.log('Message requested', message);

I added these two console logs this is the output

Moralis started
Trying to request message
error - TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ClientRequest'
    |     property 'socket' -> object with constructor 'TLSSocket'
    --- property '_httpMessage' closes the circle

this is my signin page i am using web3modal

import { signIn,useSession } from 'next-auth/react';
import { useAccount,useSignMessage,useNetwork, ConnectButton } from '@web3modal/react';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import axios from 'axios';

function SignIn() {
   const {account} = useAccount();
    const {network} = useNetwork();
    const {signMessage} = useSignMessage();
    const {status} = useSession();
    const {push} = useRouter();

    

    useEffect(() => {
        console.log(account)
        
        const handleAuth = async () => {
            const address = account.address;
            const chain = network.chain.id;
            const userData = { address: address, chain: chain, network: 'evm' }
            console.log(userData)
      
            const { data } = await axios.post('/api/auth/request-message', userData, {
              headers: {
                'content-type': 'application/json',
              },
            })
      
            const message = data.message
            
      
            const signature = await signMessage({ message })
      
            // redirect user after success authentication to '/user' page
            const { url } = await signIn('credentials', {
              message,
              signature,
              redirect: false,
              callbackUrl: '/user',
            })
            /**
             * instead of using signIn(..., redirect: "/user")
             * we get the url from callback and push it to the router to avoid page refreshing
             */
            push(url)
          }
        
        if (status === 'unauthenticated' && account.isConnected) {
          handleAuth()
        }
      }, [status, account.isConnected])

    return (
        <div>
            <h3>Web3 Authentication</h3>

           <ConnectButton />
        </div>
    );
}

export default SignIn;

can you try to log parameters that are sent to that function?

    try {
        console.log('Trying to request message');
        console.log("address",address);
        console.log("chain",chain);
        console.log("network",network);
        console.log("config",config);
        const message = await Moralis.Auth.requestMessage({
            address,
            chain,
            network,
            ...config,
        });
        console.log('Message requested', message);

and this is the output of that

Moralis started
Trying to request message
address 0xf3111b0E5De936e31D08B19925BA7c725512006D
chain 5
network evm
config {
  domain: 'http://localhost:3000/',
  statement: 'Please sign this message to confirm your identity.',
  uri: 'http://localhost:3000/',
  timeout: 60
}
error - TypeError: Converting circular structure to JSON

try to use a different variable for those parameters
and try to log the value of that variable

You need to use a different domain here, you can just use amazing.finance or anything like that.