[SOLVED] Could not make login work in Local Server Test

Hi, i have just started on your tutorials. thanks for the information and it being so well structured. i have the following question on login

here is my js code

/* Moralis init code */
const serverUrl = "https://h0yyn4fcuwhw.usemoralis.com:2053/server";
const appId = "SZKm6EvyPd4474XyYRHJjW24WPuld35rlasRfWi6";
Moralis.start({ serverUrl, appId });

/* Authentication code */
async function login() {
  let user = Moralis.User.current();
  if (!user) {
    user = await Moralis.authenticate({ signingMessage: "Log in using Moralis" })
      .then(function (user) {
        console.log("logged in user:", user);
        console.log(user.get("ethAddress"));
      })
      .catch(function (error) {
        console.log(error);
      });
  }
}


async function logOut() {
  await Moralis.User.logOut();
  console.log("logged out");
}

document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;

and i used it in this html

<!DOCTYPE html>
<html>
  <head>
    <title>Vanilla Boilerplate</title>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
  </head>

  <body>
    <h1>Moralis Hello World!</h1>

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

    <script type="text/javascript" src="./main.js"></script>
  </body>
</html>

i have changed many server configurations - testnet, mainnet. any idea why this does not work?

what happens, what doesn’t work?

there is no reaction. on the console it shows

Uncaught (in promise) ReferenceError: Moralis is not defined
    login http://127.0.0.1:5500/moralis connect.html:23
    onclick http://127.0.0.1:5500/moralis connect.html:1
moralis connect.html:23:9

you will have to add web3 too, but I don’t know exactly why you get that error in particular

or maybe you don’t need web3 any more with latest version of the SDK

are there any other errors when the page loads?

also you could try to remove the space from the name of the file

hi cryptokid, i have

  1. changed the name of the file to “connect_test.html”,
  2. changed the html as below to use the latest Moralis SDK,
<!DOCTYPE html>
<html>
  <head>
    <title>Vanilla Boilerplate</title>
    <script>src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"</script>
    <script>src="https://unpkg.com/[email protected]/dist/moralis.js"</script>  
  </head>

  <body>
    <h1>Moralis Hello World!</h1>

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

    <script type="text/javascript" src="js/main.js"></script>
  </body>
</html>

still i get the following errors on the console

now it looks like here is the wrong syntax with that src outside of script tag

1 Like

great!, its working, thank you so much, never expected the team to reply so fast. incredible work you have here, i’m impressed!

1 Like