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);
}
}