How to do authentication in Decentralized Application (DAPP)

In this blog, I will tell you about how we can add authentication to DApps

Muhammad Zaryab Rafique
Coinmonks
Published in
2 min readAug 12, 2021

--

Introduction

Blockchain technology growing day by day and it has a great impact on different sectors, also there are some challenges in Blockchain, If we talk about DApps (Decentralized Applications) the main challenge in DApps is to authenticate users, In Centralized Applications, we have different authentication providers like firebase authentication and social media login integration (Facebook, Google, Github..etc) but in DApps there are two ways to authentication user, we will discuss in detail the second one Using Smart Contract Code.

Two Ways to Authenticate user in DApp

  1. Meta Mask One-Click Login
  2. Using Smart Contract Code

Meta Mask One-Click Login:

A one-click, cryptographically secure login flow using the MetaMask extension, with all data stored on our own back end. We call it: “Login with MetaMask”.

Documentation Link: https://www.toptal.com/ethereum/one-click-login-flows-a-metamask-tutorial

Using Smart Contract Code

To authenticate the user in Dapp, I wrote a smart contract Auth.sol in Solidity language. It provides the authentication in Decentralized Application on Ethereum Blockchain using Solidity language and (Auth.sol) smart contract written to achieve the authentication functionality in Dapps.

Github Repository Link: https://github.com/Zaryab-Programmer/dapp-authentication

Smart Contract Code:

Here the contract name is Auth and inside it has struct UserDetail.

struct UserDetail {address addr;string name;string password;string CNIC;bool isUserLoggedIn;}

the mapping address => UserDetail

mapping(address => UserDetail) user;

In the User Registration function, the user can do the transaction on Meta Mask, function check that if the user does not already register by requiring check. if the user does not already register then the user data is registered on the blockchain.

function register(address _address,string memory _name,string memory _password,string memory _cnic) public returns (bool) {require(user[_address].addr != msg.sender);user[_address].addr = _address;user[_address].name = _name;user[_address].password = _password;user[_address].CNIC = _cnic;user[_address].isUserLoggedIn = false;return true;}

Login function provides the facility to log in at Dapp and changing the field isUserLoggedIn to true, means that the user logs in successfully. checkIsUserLogged function checks the user is logged in or not, it returns that boolean value that can help to authenticate the user.

function login(address _address, string memory _password) public returns (bool) {if ( keccak256(abi.encodePacked(user[_address].password)) == keccak256(abi.encodePacked(_password)) ) { user[_address].isUserLoggedIn = true; return user[_address].isUserLoggedIn;} else { return false; }
}
function checkIsUserLogged(address _address) public view returns (bool) {return (user[_address].isUserLoggedIn);}

Logout function provides the facility to logout the user by doing a transaction and changing the field of user[_address].isUserLoggedIn = false;

function logout(address _address) public {user[_address].isUserLoggedIn = false;}

By Contribution and help this blog written:

  • Mr. Amir Ali Rizvi (Blockchain Expert)
  • Lecturer at COMSATS Sahiwal
  • Mr. Nasir Mehdi
  • Lecturer at COMSATS Sahiwal

Also, Read

Join Coinmonks Telegram Channel and learn about crypto trading and investing

--

--

Muhammad Zaryab Rafique
Coinmonks

Blockchain, Rust, Go lang, Web 3.0, Ethereum, Solana, Substrate, Cosmos