-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (26 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
window.addEventListener('DOMContentLoaded', async () => {
const loginBtn = document.getElementById('loginBtn');
loginBtn.addEventListener('click', loginWithMetaMask);
});
async function loginWithMetaMask() {
const provider = await detectEthereumProvider();
if (provider) {
try {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const selectedAddress = accounts[0]; // The user's selected address
console.log('User logged in with address:', selectedAddress);
localStorage.setItem('walletAddress', selectedAddress);
window.location.href = 'dashboard.html';
} catch (error) {
console.log('Error:', error.message);
}
} else {
alert('Please install MetaMask'); // Display alert popup
}
}
ethereum.on('accountsChanged', (accounts) => {
const selectedAddress = accounts[0]; // The user's selected address
console.log('User switched to address:', selectedAddress);
const addressDisplay = document.getElementById('addressDisplay');
addressDisplay.textContent = `Logged in address: ${selectedAddress}`;
});