@tria-sdk/authenticate-web
v2.0.20
Published
## The Tria Authentication SDK provides an easy way to integrate authentication into your web applications. It includes features such as login, logout, and account management.
Downloads
883
Keywords
Readme
Tria Authentication SDK
The Tria Authentication SDK provides an easy way to integrate authentication into your web applications. It includes features such as login, logout, and account management.
Installation
To install the SDK, use the following command:
npm install @tria-sdk/authenticate-web
yarn add @tria-sdk/authenticate-web
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({
darkMode: true, // Optional: Enable dark mode (default: false)
triaStaging?: false, // Optional: Use trias staging environment (default: false)
dappName: 'Your Dapp Name', // Required: Your Dapp's name
logo: 'https://example.com/logo.svg', // Required: URL to your Dapp's logo
clientId?: 'your-client-id', // Required: Your Tria client ID for analytics
metaToken: 'your-meta-token', // Required: Your Tria meta token provided by tria
});
Configure the AuthManager
The TriaConfigOptions interface allows you to configure the following properties:
configure web 3 settings
authManager.configure({
chain: "MUMBAI",
environment: "mainnet",
aa: {
supportAa: boolean;
pimlicoApiKey?: string;
isSponsored?: boolean;
sponsorshipPolicyId?: string;
sponsorshipPolicyIds?: ChainNameToPolicyId;
},
rpcUrl: "https://my-rpc-url.com/",
dappDetails:{
dappDomain?: string;
dappLogo?: string;
}
});
Using AuthManager Methods
Login
To prompt the user to log in, call the login method:
authManager.login();
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);
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);
Usage
ensure that you build dependency packages in the following order
- build the chains package
- build the utils package
- build the connect package
run install in folder root
pnpm install
explanation of the build process
"scripts": {
"dev": "concurrently \"pnpm:vite\" \"pnpm:tw\" \"pnpm:tw-lit\"",
"vite": "tsc && vite",
"build": "tsc && vite build",
"preview": "vite preview",
"tailwind": "concurrently \"pnpm:tw\" \"pnpm:tw-lit\"",
"tw": "tailwindcss -i ./src/styles/tailwind.css -o ./src/styles/tw.css --watch",
"tw-lit": "npx twlitme --input ./src/styles/tw.css --output ./src/styles/tailwind.ts --watch"
},
The sdk itself renders a (web component)[https://lit.dev/], that uses the lit element framework to create it. Tailwind cannot be directly used here so the tw command first compiles tailwind classes used in the project to css then tw-lit converts it to javascript css which is then loaded onto the project.
The build uses vite and outputs an mjs file which is a ES Module.