Metamask is not being launched after clicked the Moralis launch button

Now it authenticate but the USER object is not being created and stored automatically in Moralis server, the User row in the table remains in 0, like all the rest.
What am I missing?

console.log('hello world');

// connect to Moralis server

Moralis.initialize("OVQv4GzkGYKOJmcd2UE0wF6AGh4Czz6Vlq6Sfzn8");

Moralis.serverURL = "https://krkxhau0hur3.usemoralis.com:2053/server";

login = async () => {

    await Moralis.authenticate().then(async function (user) {

        console.log('logged in')

        user.set("name", document.getElementById('user-name')).value;

        user.set("email", document.getElementById('user-email')).value;

    await user.save();

    })

}

document.querySelector('#btn-login').onclick = login;


You should see users added even after you only authenticate with MetaMask. Are you sure you are looking at the right Moralis server dashboard?

Yes, is the only Server I have set by now. I’m following the tutorial step by step.
I set it to “testnet”, so now I’m using Rinkeby testnet, but I changed into Ganache but it’s still not showing anything in Moralis Dashboard

This is the error I’m getting in the code with Moralis.Initialize()

Ignore it. If you correctly provided SDK import in html it will work correctly.

I’ve just tried to signup on your server. Do you see my user object?

Yes, the new user object appeared

In the Signin.html I used <script src="main.js" ></script> to connect it with the main.js where the Moralis SDK connection is, I think

You can try it yourself:

Let me know how it will work for you

I tried it, I logged in there, it lets me authenticate with Metamask but no new user showing up in the Moralis Server, there still your User as the only user.
It seems everything is working as you could logged in but there is something my code or my connection with the Moralis server. I tried changing browser to Brave and Firefox but no change

Are you trying to log in with data inputed? How does it work if you specify email and name?

Yes I’ve tried both just clicking on the button directly and by inputting the username and email, in both cases triggers Metamask, it lets me sign and that’s it.
No change in the server

Marcel could you please share over here the code you used to fix the Metamask issue? I haven’t checked my error so I haven’t fixed yet. Thanks in advance!

I’m trying to build a nft videogame for a friend and connect his marketplace with metamask, any advice or recommended docs to check?

Btw, thanks for developing this tool, this saves us tons of hours and headaches to non-coders.

Hi Billyb,

I’ve just started with Moralis and I had the same issue as you - button was unresponsive and didn’t open Metamask. To solve I kept my original code (I wrote it all out as I find it better for learning personally) and then copy and pasted the code in the tutorial into a new .html file and ran that - it ran perfectly. I ran through each line (in VS Code you can split the screen to show 2 code docs at the same time) to make sure everything matched… my issue was in

                let user = Moralis.User.current();
                if(!user) {
                    user = await Moralis.authenticate();
                }
                console.log("Logged in user:", user);

I didn’t capitalise the “U” in Moralis.User.current - changing that worked for me. If you apply a similar approach for your code you may find the issue. If you’re still having an issue let me know and I’ll test your code tomorrow - unfortunately I need to run out right now, just thought I’d jump in and try and help you quickly.

Hope this helps you resolve the issue though. :slight_smile:

1 Like

So how is your final Login object version? like this?

login = async () => {

    let user = Moralis.User.current();

    if(!user) {

    user = await Moralis.authenticate();

    }

    console.log("Logged in user:", user);

    user.set("name", document.getElementById('user-name')).value;

    user.set("email", document.getElementById('user-email')).value;

    await user.save();

    window.location.href = "dashboard.html";

    }

My code worked in the console when using both

window.location.href = "signin.html";

and

window.location.href = "dashboard.html";

and finally the server dashboard added the new user, but only by using the console, it hasn’t worked by clicking the button, and also hasn’t worked to jump from signin.html to the dashboard.html after authenticating with Metamask. It only works using the console manually

I tried your code like that but is not working either

Hi Marcel,

Does the below code run for you:

<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
        <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
    </head>
    <body>
        <h1>Gas Stats With Moralis</h1>

        <button id="btn-login">Moralis Login</button>
        <button id="btn-logout">Logout</button>

        <script>
            //connect to Moralis Server
            Moralis.initialize("ENTER APP ID");
            Moralis.serverURL = "ENTER SERVER";
            
            async function login() {
                let user = Moralis.User.current();
                if(!user) {
                    user = await Moralis.authenticate();
                }
                console.log("Logged in user:", user);
            }

            async function logOut() {
                await Moralis.User.logOut();
                console.log("Logged Out");
            }

            document.getElementById("btn-login").onclick = login;
            document.getElementById("btn-logout").onclick = logOut;
        </script>
    </body>
</html>

That should work for you after you enter your server and app ID. if it doesn’t the next thing I would try is to open the page in Chrome with no active extensions except Metamask. I have found that sometimes Chrome extensions can cause a number of issues with web pages - and as a dev I like to turn off as many as I can when debugging. If it does work for you without any other extensions then you can turn on each one, one by one and keep testing until you find the one causing an issue.

Other than that I’m not really sure, as I say I’m still new to Moralis but if I come across anything further down the line that might help I’ll let you know.

2 Likes

Yes, this code is working and Moralis server updated with new user.

So you put everything into one single html file adding the main.js as a script into the signin.html?

Currently I’m just following this guide https://docs.moralis.io/guides/build-a-simple-dapp-in-3-minutes which is the same as Billyb so I’ve not got onto having multiple pages/files. However since this test worked, it would lead me to believe that it certainly has something to do with the fact you have this split out over multiple files.

I should probably also point out that I’m learning JS at the same time as learning Moralis so I’m not totally sure how these additional files would normally interact with each other. I would however think that doing what you’ve suggested above would be the best place to start and see if you can get a result that way.

Hi Marcel,
I noticed you posted this:

The syntax is incorrect as the .value is outside of the user.set method.
In this case it should be:

    user.set("name", document.getElementById('user-name').value);
    user.set("email", document.getElementById('user-email').value);

Notice the final parenthesis is different from yours.
These minor differences can throw the whole function out.

Thanks. I edited it but there has been no change. I tried everything.
Sign out button at dashboard.html also not redirecting to sign in html.
None of the tables on dashboard html are showing and not being linked to external page.

Am I right in thinking that you are naming your files “Dashboard.html” and “Signin.html”, but you are trying to send the user to “dashboard.html” and “signin.html” ?

Because incorrect capitalisation will cause a problem