MetaMask login is the standard authentication method for decentralized applications (dApps). It allows users to connect their Ethereum wallet and interact with blockchain networks securely without sharing private keys.
MetaMask has become the de facto standard for Ethereum wallet interaction, with over 30 million monthly active users. Implementing MetaMask login enables your dApp to:
Before implementing MetaMask login, ensure you have:
Choose between Web3.js or Ethers.js. Ethers.js is recommended for new projects due to its smaller bundle size and better TypeScript support.
# Install Ethers.js (recommended)
npm install ethers
# Or install Web3.js
npm install web3
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="text/javascript"></script>
The core of MetaMask login is the eth_requestAccounts method, which prompts the user to connect their wallet.
async function connectMetaMask() {
// Check if MetaMask is installed
if (typeof window.ethereum !== 'undefined') {
try {
// Request account access
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts'
});
const account = accounts[0];
document.getElementById('wallet-address').innerText =
`Connected: ${account.slice(0, 6)}...${account.slice(-4)}`;
console.log('MetaMask login successful:', account);
return account;
} catch (error) {
console.error('User rejected MetaMask login:', error);
alert('MetaMask login failed. Please try again.');
}
} else {
alert('Please install MetaMask to use this dApp!');
window.open('https://metamask.io/download/', '_blank');
}
}
Design an intuitive button that clearly indicates the wallet connection action.
<button id="metamask-login-btn" class="btn" onclick="connectMetaMask()">
<img src="metamask-fox.svg" alt="MetaMask" width="24" height="24" style="vertical-align: middle; margin-right: 8px;">
Connect with MetaMask
</button>
<div id="wallet-address" style="margin-top: 15px; font-weight: bold;"></div>
Users may switch networks or accounts in MetaMask. Listen for these events:
// Listen for account changes
window.ethereum.on('accountsChanged', (accounts) => {
if (accounts.length === 0) {
console.log('Please connect to MetaMask.');
document.getElementById('wallet-address').innerText = 'Disconnected';
} else {
const account = accounts[0];
document.getElementById('wallet-address').innerText =
`Connected: ${account.slice(0, 6)}...${account.slice(-4)}`;
console.log('Account changed:', account);
}
});
// Listen for chain changes
window.ethereum.on('chainChanged', (chainId) => {
console.log('Network changed to:', chainId);
window.location.reload(); // Reload to update chain-specific data
});
For enhanced security, implement nonce-based signing to prevent replay attacks:
async function signMessage(account) {
const nonce = Date.now().toString();
const message = `Sign this message to verify your identity.\nNonce: ${nonce}`;
try {
const signature = await window.ethereum.request({
method: 'personal_sign',
params: [message, account]
});
// Send signature and message to backend for verification
const response = await fetch('/api/verify-signature', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message, signature, address: account })
});
if (response.ok) {
alert('MetaMask login with signature verified!');
}
} catch (error) {
console.error('Signing failed:', error);
}
}
Security Tips:
window.ethereum is checked properlyWhile this guide focuses on MetaMask login, you can extend support to other wallets:
const providers = [
window.ethereum, // MetaMask, Trust Wallet, etc.
window.binancechain, // Binance Chain Wallet
// Add more providers
];
async function connectWallet() {
for (const provider of providers) {
if (provider) {
try {
const accounts = await provider.request({
method: 'eth_requestAccounts'
});
return accounts[0];
} catch (error) {
continue; // Try next provider
}
}
}
throw new Error('No wallet found');
}
Implementing MetaMask login is a crucial step in building user-friendly decentralized applications. By following this guide, you can create a secure, seamless wallet connection experience that meets industry standards.
Remember these key points:
With proper MetaMask login integration, your dApp will provide users with a familiar, secure, and efficient way to interact with the blockchain ecosystem.