Login to MetaMask with ethers

Login to MetaMask with ethers

Okay, this might seem really straight forward, but I actually had some problems figuring this out. I just started learning how to build dapps and found myself ripping my hair out because I couldn't figure out such a simple thing.

Logging in with your crypto wallet is one of the most basic features of a dapp, so knowing how to do it is an essential part of working with Web3. I'm going to walkthrough how I solved this, and the potential pitfalls a newcomer might come across.

No built in method

I later found out that there is no actual method in the ethers library that triggers this process.

The solution

async function login(){ // Get the metamask provider which exists in `window.ethereum` const provider = new ethers.providers.Web3Provider((window as any).ethereum); // Use the `eth_requestAccounts` method to trigger metamask signing await provider.send("eth_requestAccounts", []); const signer = provider.getSigner(); // Get the address const address = await signer.getAddress(); // Get the balance const balance = await signer.getBalance(); }

It is as simply as that.

Alternative solution (but don't use it)

If you are searching for how to login in with ethereum using ethers, you might come across another solution. That solution looks like this:

async function login(){ // This will trigger the same process (window as any).ethereum.enable(); // Get the metamask provider which exists in `window.ethereum` const provider = new ethers.providers.Web3Provider((window as any).ethereum); const signer = provider.getSigner(); // Get the address const address = await signer.getAddress(); // Get the balance const balance = await signer.getBalance(); }

That's it

That's all I had to say about signing in with ethereum using the ethers library. It's not complicated, but if you expect it to exist as a method in the library it might be confusing.