Skip to main content

How to Integrate OpenLogin and Avalanche


This tutorial will guide you over a basic example to integerate Openlogin authentication with avalanche.

We will go through a react app where user can login,create avalanche account, fetch account's balance and logout.

You can find the source code of this is example on Github.

Register your OpenLogin application#

In order to use OpenLogin SDK, you'll need to create a project in Developer Dashboard and get your client ID.

App registration is not required for localhost development.

Let's get started with code by installing depedencies using npm#

To start with using openlogin with a avalanche dapp , you need to install Openlogin and avalanche sdk.

npm install --save @toruslabs/openlogin
npm install --save avalanche

Connect with avalanche testnet#

const myNetworkID = 5;
const avalanche = new Avalanche("", 443, "https", myNetworkID);

Create and initialize openlogin instance#

Start with creating a instance of openlogin class and initialize it using openlogin.init() when application is mounted. After initialization it checks if sdk has private key then user is already logged in.

We are using two options while creating openlogin class instance:-

  • clientId: clientId is a public id which is used to to identify your app. You can generate your client id from developer dashboard. For localhost you can use any static random string as client id.

  • network: network can be testnet or mainnet.

useEffect(() => {
const sdkInstance = new OpenLogin({
clientId: "YOUR_PROJECT_ID",
network: "testnet"
async function initializeOpenlogin() {
await sdkInstance.init();
if (sdkInstance.privKey) {
// app has access to private key now
}, []);


Once the sdk is initialized , then openlogin.login will be called when user clicks login button.

It will start the login flow for the user. Openlogin sdk provides two UX modes (ie POPUP and REDIRECT) for login flow. You can use either depends on your application UX by setting up uxMode option in login function, default is redirect.

Note: POPUP mode is coming soon.

In redirect mode user will be redirected completely out of app and will be redirected back to redirectUrl after successfull authentication, application will have access to private key as openlogin.privKey after intializing openlogin instance.

In PopUp mode, openlogin authenication window will open as a popup and app will get private key when openlogin.login promise will resolve.

This example is compatible with both redirect and popup ux modes.

In the given code snippet, openlogin.login function is getting called along with two options:-

  • loginProvider: It specifies the login method which will be used to authenticate user. You can checkout API Reference to know about all supported and custom login provider values.

  • redirectUrl: User will be redirected to redirectUrl after login.

Checkout API Reference for other options available to pass in openlogin constructor and login function.

async function handleLogin() {
try {
const privKey = await openlogin.login({
loginProvider: "google",
redirectUrl: `${window.origin}`,
await importUserAccount(privKey);
} catch (error) {
console.log("error", error);

Use the private key with web3#

Cde snippet given below ,connects with avalanche test network using avalanche library, imports a account with private key and fetches imported account balance of some previously deployed test asset.

async function importUserAccount(privateKey) {
const myNetworkID = 5;
const avalanche = new Avalanche("", 443, "https", myNetworkID);
const xchain = avalanche.XChain(); //returns a reference to the X-Chain used by AvalancheJS
const myKeychain = xchain.keyChain();
const importedAccount = myKeychain.importKey(Buffer.from(privateKey,"hex")); // returns an instance of the KeyPair class
let address = importedAccount.getAddressString()
const myAddresses = xchain.keyChain().getAddressStrings();
const u = await xchain.getUTXOs(myAddresses);
const utxos = u.utxos
const assetid = "8pfG5CTyL5KBVaKrEnCvNJR95dUWAKc1hrffcVxfgi8qGhqjm"; // random cb58 string
const mybalance = utxos.getBalance(myAddresses, assetid);
console.log(mybalance, address);
setUserAccountInfo({balance: mybalance.toNumber(), address})

Log out handler#

In order to logout user you needs to call logout function available on sdk instance. Logout function will clears the sdk state and removes any access to private key on frontend.

You can pass various other options in logout function like fastLogin , redirectUrl etc. To know more about that checkout API Reference

const handleLogout = async () => {
await openlogin.logout();


You can checkout example of this example app here.the source code of this is example on Github.