Build a Simple dApp in 3 Minutes - Login (Part 2)

Build Your First Moralis dApp! This Guide Series Covers the Basics and How to Get Started Fast. In Part Two, We Implement User Login with MetaMask.

How to Login with MetaMask

Install MetaMask if you haven't already. Add the following code to the <script> tag below the Moralis init code.

// connect to Moralis server
Moralis.initialize("paste Moralis APP ID here");
Moralis.serverURL = "paste Morlis server URL here";
// add from here down
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;

Run dApp on Localhost

Run index.html on localhost as a web page. The easiest way is by using the live server extension Visual Studio Code. Just right click on index.html and select Open with Live Server.

Click Login

You will see a page like the following. Clicking the Moralis Login button will call Moralis.authenticate() and prompt the user to connect MetaMask.

Sign a message to prove address ownership.
Press the login button, then enter your MetaMask password.

Open the JavaScript console by pressing F12 . You should see a message like the following. Congratulations, your dApp has it's first user!

Logging Out

Pressing the Logout button will call Moralis.User.logOut() . It's that easy.


Wowww! Congratulations! You have a login working! Keep the pamp going...