window.addEventListener('load', function () {
$.ajax(
{
type: 'GET',
dataType: 'JSON',
url: '/GetMoralisConfig',
success:
function (response) {
Moralis.initialize(response.appId);
Moralis.serverURL = response.server;
},
error:
function (response) {
alertMessage("alertDanger", "Error : Please contact admin! Code:01");
}
});
if (window.localStorage.walletconnect) {
Moralis.enable({
provider: "walletconnect"
});
} else {
Moralis.enable();
}
});
const ethereumButton = document.querySelector('.enableEthereumButton');
const showAccount = document.querySelector('.showAccount');
Moralis.Web3.onAccountsChanged(function (accounts) {
login();
});
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
window.localStorage.removeItem('WALLETCONNECT_DEEPLINK_CHOICE');
}
});
async function login() {
let message = 'Hello, You are logging in : ' + (Math.random() + 1).toString(36).substring(2);
var walletProvider = "walletconnect";
// Check if Web3 has been injected by the browser (MetaMask).
if (window.ethereum) {
walletProvider = "metamask";
}
$.LoadingOverlay("show");
Moralis.Web3.authenticate({ signingMessage: message, provider: walletProvider, chainId: 56 }).then(function (user) {
authenticate(user.id, user.get('ethAddress'));
}).catch((err) => {
alert("Error : Please contact admin! Code:02 - " + err.message);
$.LoadingOverlay("hide");
});
}