Implement Ethereum Wallet Authentication (e.g. Metamask)
You can easily implement an ethereum wallet based login and authentication flow with Streambird Wallet API for wallets such as MetaMask. Alternatively, you can also use our wallet API to complement your existing authentication flow as a multi-factor authentication as shown here.
This example assumes that you are using the Streambird Auth API in your backend using your Streambird ApiKey
that has access to your entire App
on Streambird.
1 - Implement Wallet Login UI
In this example, we will be using MetaMask; however, since all of Streambird platform is API first, we are compatible with any wallet that can sign a message.
Here is the login flow we are trying to achieve where user will
- Log in
- Connect and verify wallet address
- Use app or dApp
Metamask login flow
Screen to sign in with Metamask or add a wallet to an existing account if you want to use it as a secondary factor of authentication similar to social OAuth logins like Google and Apple.
Metamask Button UI
2 - Retrieve Wallet Address from MetaMask
Since most Ethereum users depend on MetaMask, the easiest way to retrieve the the Ethereum wallet address is via MetaMask. You can also easily check if your user has MetaMask installed by calling window.ethereum
and ensuring that it is not undefined. If MetaMask is not installed, the many live codeblocks throughout this guide will not work.
Live Example of Retrieving Wallet Address from MetaMask
Click on the Connect
button to initiate a MetaMask connection and retrieve your wallet address.
3 - Register user or sign in user
Each user must be stored on Streambird Auth, so we recommend ensuring that you store our auto generated user ID from the response into your database/backend in a column or field against that user (as long as you can associate your user with the auto generated ID returned by Streambird).
We will ensure that each wallet address is ONLY attached to a single user at any time. We will be using the BeginWalletRegistration where if a user ID is sent in, we will associate it with the user if it is not already associated with another user. Otherwise, a new user will be created on the fly (aka JIT, Just in time).
The registration request will create a challenge
that must be signed by the wallet private key associated with the public address you are intending to register. You can use this single endpoint to handle both registration and sign in since as long as the same user ID is associated with the wallet address, you can treat it as a LoginOrCreateUser
endpoint for ethereum wallet address.
4 - Sign Wallet Challenge Message
In the previous step, Streambird will return a challenge
for you to sign and user_id
associated with the wallet address. If it is a new user, we recommend that you attach the user_id
to your own user in your database.
You can now choose any ethereum compatible wallet to sign this message. In this example, we will be using MetaMask as shown below. Here are some great resources from MetaMask for your convenience to get started and learning more about signing.
Signature Request
To begin development with our API without building your own MetaMask integration, you can use mycrypto.com
to easily trigger a MetaMask signing of your challenge here, choose MetaMask
, then paste in the challenge. Once signed by MetaMask, use the sig
field in the JSON to continue with this tutorial. For your convenience, you can also use our live codeblock below and simply replace the message with your own challenge message.
Full Live Example of Login to MetaMask with Message Signing
5 - Verify Wallet Registration
Once you have signed your challenge
, you need to send the signature
, wallet_type
, and the public_address
of the wallet you are verifying to the VerifyWalletRegistration endpoint.
You will receive a success response like the following
Now you can choose to resume to your existing login flow and issue a session token for this user.
Optionally, if you want to use Streambird’s Session management, you can request Streambird to issue you a session token in the VerifyWalletRegistration step by sending in a session_expires_in
parameter like the following.
A successful response will contain a session
object with session_token
and session_jwt
like the following response. Your can read more about Session management here on how to leverage Streambird’s Session management for multifactor authentication flow.
In the case where the signature is invalid, we will return
You can return or display this error to your user via your API or application.
Congrats! You have now integrated ethereum login into your application without building and maintaining additional infrastructures to manage your Web3 credentials. Let us take care of authentication and you can focus on your core product.