I am trying to connect self hosted server in moralis version 2 but authentication is failed
also showing Error: Moralis auth failed, invalid data when i am using moralis-v1 or version 2
export async function startMoralis() {
const serverUrl = "http://localhost:1337/server";
const appId = "001";
Moralis.start({ serverUrl, appId});
}
async function login() {
let user = Moralis.User.current();
if (!user) {
user = await Moralis.authenticate({
provider: wallet_name,
signingMessage: "Log in using Moralis",
})
.then(async function (user) {
user_address = user.get("ethAddress").substring(0, 6);
const chainId = await Moralis.chainId;
const accepted_chains = ["0x61", "0x38"];
if (accepted_chains.includes(chainId)) {
chain = chainId;
} else {
try {
await Moralis.switchNetwork("0x38");
} catch {
alert(
"Could not connect to Metamask. Please try switching chains."
);
}
}
// Subscribe to onChainChanged events
const unsubscribe = Moralis.onChainChanged(async (chainId) => {
if (accepted_chains.includes(chainId)) {
chain = chainId;
} else {
try {
await Moralis.switchNetwork("0x38");
} catch {
alert(
"Could not connect to Metamask. Please try switching chains."
);
}
}
});
})
.catch(function (error) {
console.log(error);
});
}
}
async function handleWalletClick() {
let user = Moralis.User.current();
if (!user) {
await login();
} else {
await logOut();
user_address = null;
}
}
export async function initMoralis() {
await Moralis.enableWeb3();
user_address = Moralis.User.current().get("ethAddress").substring(0, 6);
const chainId = await Moralis.chainId;
const accepted_chains = ["0x61", "0x38"];
if (accepted_chains.includes(chainId)) {
chain = chainId;
} else {
try {
await Moralis.switchNetwork("0x38");
} catch {
alert("Could not connect to Metamask. Please try switching chains.");
}
}
// Subscribe to onChainChanged events
const unsubscribe = Moralis.onChainChanged(async (chainId) => {
const accepted_chains = ["0x61", "0x38"];
if (accepted_chains.includes(chainId)) {
chain = chainId;
} else {
try {
await Moralis.switchNetwork("0x38");
} catch {
alert("Could not connect to Metamask. Please try switching chains.");
}
}
});
}