@tria-sdk/authenticate-web

The @tria-sdk/authenticate-web module facilitates user onboarding for your dapp by providing support for social (e.g., Google, Facebook), mobile, email, and @tria and password signup and logins, along with full customizability to tailor the design and flow to your specific dApp requirement. This library is meant to be framework agnostic, and can be used in any web application on the client side. The library can be used in a few different ways, either via a script tag and importing as a module or cdn link, or installing via npm or yarn and importing into your project.

Installation

You can install the SDK using npm, yarn or via a script tag.:

npm install @tria-sdk/authenticate-web
yarn add @tria-sdk/authenticate-web
<script type="module">
  import {AuthManager} from
  "https://unpkg.com/@tria-sdk/[email protected]/dist/index.es.js";
</script>

Usage

Importing the AuthManager

First, import the AuthManager class from the SDK:

import { AuthManager } from '@tria-sdk/authenticate-web'

Initializing the AuthManager

Create an instance of the AuthManager with the required options:

const authManager = new AuthManager({
  analyticsKey: {
    clientId: 'string', // Required: Provided by Tria
    projectId: 'string', // Required: Provided by Tria
  },
})

Call the ready() method on AuthManager instance. This method needs to be called after setting the configuration options on the newly created AuthManager Instance

authManager.ready()

Example

Here's an example of how you might use the Tria Authentication SDK in a web application:

import { AuthManager } from '@tria-sdk/authenticate-web'

const authManager = new AuthManager({
  analyticsKey: {
    clientId: 'string',
    projectId: 'string',
  },
})

authManager.ready();

const handleLogin = (): void => {
      console.log("calling handleLogin");
      const resp = authManager.getAccount();
      console.log(resp);
    };

authManager.subscribe("LOGIN_SUCCESS", handleLogin);

// show login modal
authManager.login()

Configuring the AuthManager

Configuration Options

The TriaConfigOptions interface allows you to configure the following properties:

  • chain [string]: The blockchain network (e.g., 'MUMBAI', 'POLYGON').
  • customChain [object]: Optional custom chain data.
  • environment [string]: The environment ('testnet' or 'mainnet').
  • dappDetails [object]: An object containing your Dapp's domain and logo.
  • aa [object]: Optional Account Abstraction details.
  • rpcUrl [string]: Optional RPC URL for the blockchain network.
  • supportedChains [string array]: An array of strings containing the supported chains

// uasage shown below
 authManager.configure({
     chain: "SEPOLIA", // required
   });

// interface and types shown below for extended configuration
interface TriaConfigOptions {
  chain: string;
  customChain?: CustomChainData;
  environment?: ENV;
  dappDetails?: dappDetails;
  aa?: AaDetails;
  rpcUrl?: string;
  supportedChains?: string[];
}

Using AuthManager Methods

Login

To prompt the user to log in, call the login method:

const handleLogin = (): void => {
      console.log("calling handleLogin");
      const response = authManager.getAccount();
      console.log(response);
    };

authManager.subscribe("LOGIN_SUCCESS", handleLogin);
authManager.login();


// The response structure is shown below
interface LoginResponse {
  method: string; // login or signup
  success: boolean; // success or false
  platform: string; // google twitter etc
  name: string; // users name if signedup with socials
  loginId: string; // login id of the user
  sessionToken?: string; // session token will be provided only on signups
  account?: Account | null; // account object
}

interface Account = {
  triaName: string | null;
  evm: {
    address: string;
  };
  nonEvm: [
    {
      chainName: string;
      address: string;
    }
  ];
  aa: {
    address: string;
  };
};

Logout

To log the user out, call the disconnect method:

await authManager.disconnect()

Get Account

To get the user's account information, call the getAccount method:

const account = authManager.getAccount()

This method returns an Account object with the following properties:

  • triaName: The user's Tria username.
  • evm: An object containing the user's EVM-compatible address.
  • nonEvm: An object containing the user's non-EVM-compatible address.

Check Authentication Status

To check if the user is authenticated, call the isAuthenticated method:

const isAuthenticated = authManager.isAuthenticated()

This method returns a boolean indicating whether the user is logged in.

Send Transaction

To send a transaction, call the send method with the amount, recipient address, and optional token address:

await authManager.send(amount, recipientAddress, tokenAddress)

Sign Message

To sign a message, call the signMessage method with the message:

const signature = await authManager.signMessage(message)

Write Contract

To write to a smart contract, call the writeContract method with the contract details and optional payment token:

await authManager.writeContract(contractDetails, payToken)

Read Contract

To read from a smart contract, call the readContract method with the contract details:

const result = await authManager.readContract(contractDetails)

Send NFT

To send an NFT, call the sendNft method with the recipient's Tria name and NFT details:

await authManager.sendNft(recipientTriaName, nftDetails)

Event Listeners

You can listen to login and logout events by adding event listeners:

authManager.addEventListener('TRIA_LOGIN', (event) => {
  // Handle login event
})

authManager.addEventListener('TRIA_LOGOUT', (event) => {
  // Handle logout event
})

Removing Event Listeners

To remove an event listener, use the removeEventListener method:

authManager.removeEventListener('TRIA_LOGIN', loginHandler)
authManager.removeEventListener('TRIA_LOGOUT', logoutHandler)

Telegram Mini Apps

To use the sdk in miniapp environment make sure the app is initialised properly according to steps mentioned here https://core.telegram.org/bots/webapps

Was this page helpful?