It works fine now thanks
But after i added the function to the code, when i connect it display the profile and the logout button automatically unless i refresh the page
Hello are you there?
Yes, you also have this code:
let user = Moralis.User.current();
if (!user) {
btnlogin.style.display = "block";
} else {
btnlogin.style.display = "none";
}
That doesn’t use await
You can do some debugging to see what it is going on, you can look in browser console to see if there are errors, you can use console log in your code to see if the code is executed after authentication.
You mean i should await to the code below just as like this?
let user = await Moralis.User.current();
if (!user) {
btnlogin.style.display = "block";
} else {
btnlogin.style.display = "none";
}
Yes, on that wait I was referring, you could integrate here in your original code the logic for logout button too
This is what i did and my profile button and logout button is not hidden when i am open the website on my browser and also i am not seeing any console messages anymore in the console tab and my sign in custom messages is not coming up anymore.
Please take a look at my code and see what am doing wrong. Thanks in Advance.
const serverUrl = "https://mzrhanzmogsu.moralishost.com:2053/server";
const appId = "gFnTLmvgt8ZDUYMXF80FIonsCRqosKxXcfzmO2bM";
Moralis.start({ serverUrl, appId });
const btnlogin = document.getElementById("btn-login");
async function current() {
let user = await Moralis.User.current();
if (!user) {
document.getElementById("btn-login").style.display = "block";
document.getElementById("btn-logout").style.display = "none";
document.getElementById("btn-UserInfo").style.display = "none";
} else {
document.getElementById("btn-login").style.display = "none";
document.getElementById("btn-logout").style.display = "block";
document.getElementById("btn-UserInfo").style.display = "block";
}
}
async function login() {
if (!user) {
user = await Moralis.authenticate({
signingMessage: "Welcome to MrGrandPaDoge NFT MarketPlace | The OG of Bitcoin",
});
if (user) {
btnlogin.style.display = "none";
}
}
console.log("logged in user:", user);
console.log("logged in");
const isWeb3Active = Moralis.ensureWeb3IsInstalled();
Moralis.onAccountsChanged(async(accounts) => {
console.log("ACCOUNTS CHANGED");
const confirmed = confirm(
"Do you want to link this address to your account?"
);
if (confirmed) {
await Moralis.link(accounts[0]);
}
});
if (isWeb3Active) {
console.log("Activated");
} else {
async function enable() {
await Moralis.enable();
}
}
}
async function logOut() {
await Moralis.User.logOut();
btnlogin.style.display = "block";
console.log("logged out");
}
login = async() => {
try {
await Moralis.authenticate();
} catch (error) {
alert(error);
}
};
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
document.getElementById("btn-UserInfo").onclick = Profile;
it doesn’t look like you call this function
you could call it in your main.js file
I did that before check the code again bro
where did you do that, I don’t see it, it may be also confusing that you use current for a function name and there is also Moralis.User.current()
Okay this is actually my main.js code what i want to do is to hide my logout button and my profile button before connecting to my metamask and only show the connect wallet button.
Please how do i do that in this code below. Thanks.
const serverUrl = "https://mzrhanzmogsu.moralishost.com:2053/server";
const appId = "gFnTLmvgt8ZDUYMXF80FIonsCRqosKxXcfzmO2bM";
Moralis.start({ serverUrl, appId });
async function login() {
let user = Moralis.User.current();
if (!user) {
user = await Moralis.authenticate({
signingMessage: "Welcome to GrandPaDoge",
});
}
console.log("logged in user:", user);
const isWeb3Active = Moralis.ensureWeb3IsInstalled();
if (isWeb3Active) {
console.log("Activated");
} else {
async function enable() {
await Moralis.enable();
}
}
}
async function logOut() {
await Moralis.User.logOut();
console.log("logged out");
}
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
document.getElementById("btn-userinfo").onclick = profile;
I would add this code
it works by adding this code but when i am connected to my metamask, it doesn’t show the logout and the profile button automatically, until i refresh the page then it will show, Please how can you help me to make it show automatically when i connected my wallet.
This is my new code below.
const serverUrl = "https://mzrhanzmogsu.moralishost.com:2053/server";
const appId = "gFnTLmvgt8ZDUYMXF80FIonsCRqosKxXcfzmO2bM";
Moralis.start({ serverUrl, appId });
async function hide_buttons() {
let user = await Moralis.User.current();
if (!user) {
document.getElementById("btn-login").style.display = "block";
document.getElementById("btn-logout").style.display = "none";
document.getElementById("btn-UserInfo").style.display = "none";
} else {
document.getElementById("btn-login").style.display = "none";
document.getElementById("btn-logout").style.display = "block";
document.getElementById("btn-UserInfo").style.display = "block";
}
}
hide_buttons();
async function login() {
let user = Moralis.User.current();
if (!user) {
user = await Moralis.authenticate({
signingMessage: "Welcome to GrandPaDoge NFT MarketPlace",
});
}
console.log("logged in user:", user);
const isWeb3Active = Moralis.ensureWeb3IsInstalled();
if (isWeb3Active) {
console.log("Activated");
} else {
async function enable() {
await Moralis.enable();
}
}
}
async function logOut() {
await Moralis.User.logOut();
console.log("logged out");
}
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
document.getElementById("btn-userinfo").onclick = profile;
Take a look at the above code bro
it works by adding this code but when i am connected to my metamask, it doesn’t show the logout and the profile button automatically, until i refresh the page then it will show, Please how can you help me to make it show automatically when i connected my wallet.
you can add here
hide_buttons();
after that console.log("logged in user:", user);
line.
After adding this code, when i click on connect wallet, it will automatically show the logout button and the profile, but when i clicked on the logout button it won’t show the connect button, please how can i do this?
You add same line that hides buttons at the end of logout function.
Okay i will do that now
It works perfectly now thank you. You guys need to open a trustpilot account so that we can give you guys a review. Moralis is a great platform to start your coding environment for free