[SOLVED] MoralisError [Moralis SDK Error]: [C0009] Modules are started already. This method should be called only one time

Hey guys i am using moralis Auth for a nextjs project and seem to have an issue

 MoralisError [Moralis SDK Error]: [C0009] Modules are started already. This method should be called only one time.

whenever I reset the project and run it works but if I don’t do that I get an error above

NEXT AUTH

import CredentialsProvider from 'next-auth/providers/credentials';
import NextAuth from 'next-auth';
import Moralis from 'moralis';

export default NextAuth({
    providers: [
        CredentialsProvider({
            name: 'MoralisAuth',
            credentials: {
                message: {
                    label: 'Message',
                    type: 'text',
                    placeholder: '0x0',
                },
                signature: {
                    label: 'Signature',
                    type: 'text',
                    placeholder: '0x0',
                },
            },
              async authorize(credentials) {
                try {
                  // "message" and "signature" are needed for authorisation
                  // we described them in "credentials" above
                  const { message, signature } = credentials;

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

                  const { address, profileId } = (
                    await Moralis.Auth.verify({ message, signature, network: 'evm' })
                  ).raw;

                  const user = { address, profileId, signature };
                  // returning the user object and creating  a session
                  return user;
                } catch (e) {
                  console.error(e);
                  console.log('Error in Authorize')
                  return null;
                }
              },
        }),
    ],
    // adding user info to the user session object
    callbacks: {
        async jwt({ token, user }) {
            user && (token.user = user);
            return token;
        },
        async session({ session, token }) {
            session.user = token.user;
            return session;
        },
    },
});

REQUEST MESSAGE

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;
    console.log(address, chain, network, "address, chain, network");
    console.log(config, "d");
    
    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);


        res.status(200).json(message);
    } catch (error) {
        res.status(400).json({ error});
        //if res.status 500 console.log(error)



        console.error(error);

    }
}

What is your package.json? There is also a newer version of the Moralis/Next.js/NextAuth tutorial - if possible, you should switch to using that newer code.

Hi!

I have the same problem, I’m trying to do some debuging today and
I’ll come back with an answer :slight_smile:

it seems that with this new tutorial, everything works fine! @michelfawaz

Yes just tested new tutorial fixed the issue👍🏻

having the same problem just initializing the moralis it goes above error

Are you using the new tutorial code?

yes same code event api

Try cloning and installing the demo repo - make sure to copy over your .env.local file.

my question now is that how can I link this to a database similar to the previous version of this code

The previous version of the Next.js tutorial didn’t use a database. There’s a few tutorials you can look at if you want to integrate with a backend provider such as Firebase or you can connect to the database that you’re using in your app (with something like Prisma).

Closing due to the other topic you created and core issue is solved.

1 Like