Add Authentication for Fortmatic's Ethereum Wallet to a React Application
This lesson is meant to be an introduction to using the Web3 API inside of a React Application. By the end, you'll have learned how to hook up communication between an user's Fortmatic's Wallet and the Ethereum's Rinkeby Test Network.
Duration
2 min
Access
Free
Transcript
Available
This lesson is meant to be an introduction to using the Web3 API inside of a React Application. By the end, you'll have learned how to hook up communication between an user's Fortmatic's Wallet and the Ethereum's Rinkeby Test Network.
Thomas Greco: Above our wallet component, I'm just going to create this initializeFortmatic function. In here, I'm just going to create this fm variable and set it equal to our Fortmatic constructor that we've imported above. It's important to note that I'm passing in my API key here. Otherwise, this isn't going to work.
This fm constant will allow us to interact with the wallet that Fortmatic provides us with. Then from here, use Fortmatic's getProvider method to inform web3 of which Ethereum blockchain to connect to. By default, Fortmatic is going to hook us up to the Rinkeby testnet.
Once we have that locked up, we could use web3.eth module, and thus all of these methods it provides us with, but we're just going to say getAccounts. getAccounts is going to return a promise. We're going to chain an event on it, and then we're just going to log the response of this promise.
Before we could do that, we need to use the useEffect hook inside here, and then we're only going to want this to fire once the component mounts. Therefore, we're going to use an empty dependency array.
Fortmatic is already picking up my credentials because I've already logged in, so I'm just going to quickly kill my server. If I fire it back up and I try to visit our application, we'll see that Fortmatic's dialogue is, in fact, showing up for us. It already has my email ready for me, and I'll pass the password in. It's going to give me a nice little welcome back sign.
More importantly, it's going to log our wallet address just like we set it up to.