Usage of Single Factor Auth JS SDK
Once you've installed and successfully initialized Web3Auth, you can use it to authenticate your users. Further, you can use the native provider given by Web3Auth to connect the users to the respective blockchain network.
Functionality Overview
Natively, the instance of Web3Auth (called web3auth in our examples) has the following methods/getters:
Name | Description |
---|---|
connect | Use this method to login a user to Web3Auth SFA JS SDK. |
provider | Returns the native provider that can be used to make different blockchain transactions. |
sessionId | Returns the sessionId of the user as a string. |
authenticateUser | Returns a promise of the UserAuthInfo object containing the idToken of the user. |
addChain | Add chain configuration to the web3auth instance. |
switchChain | Switches the chainId to one of the added chainIds. |
getUserInfo | Returns the user information. |
logout | Log out the user from the web3auth instance. |
Logging in your User
To log in a user using the Web3Auth SFA JS SDK, call the connect
method. This method returns an
IProvider
instance upon successful authentication which can then be used to interact with various
blockchain networks. For more details, refer to the provider documentation.
Please refer to the Authentication section for more details on the setting up your verifier and other authentication parameters.
LoginParams
The method accepts LoginParams
as an input.
- Table
- Type Declarations
Parameter | Description |
---|---|
verifier | Name of the verifier. It's a mandatory parameter as a string. |
verifierId | Verifier ID's value, sub or email value present in the idToken. It's a mandatory parameter as a string. |
idToken | A newly created JWT Token that has not already been sent to Web3Auth or a Duplicate Token error will be thrown. It's a mandatory parameter as a string. |
subVerifierInfoArray? | Sub verifier info. It's an optional parameter as a TorusSubVerifierInfo[]. |
serverTimeOffset? | Server time offset. It's an optional parameter as a number. |
export type LoginParams = {
verifier: string;
verifierId: string;
idToken: string;
subVerifierInfoArray?: TorusSubVerifierInfo[];
serverTimeOffset?: number;
};
TorusSubVerifierInfo
- Table
- Interface
Name | Description |
---|---|
verifier | Name of the verifier. It's a string mandatory parameter. |
idToken | A newly created JWT Token that has not already been sent to Web3Auth or a Duplicate Token error will be thrown. It's a string mandatory parameter. |
export interface TorusSubVerifierInfo {
verifier: string;
idToken: string;
}
Usage
- Single Verifier
- Aggregate Verifier
await web3auth.connect({
// Get the verifier name from the Web3Auth Dashboard
verifier: "YOUR_VERIFIER_NAME",
// Pass the JWT token verification value selected for verifier.
verifierId: "YOUR_VERIFIER_ID",
// Pass your JWT token
idToken: "YOUR_JWT_TOKEN",
});
await web3auth.connect({
// Get the aggregate verifier name from the Web3Auth Dashboard
verifier: "YOUR_AGGREGATE_VERIFIER_NAME",
// Pass the JWT token verification value selected for sub verifier.
verifierId: "YOUR_VERIFIER_ID",
// Pass your JWT token
idToken: "YOUR_JWT_TOKEN",
subVerifierInfoArray: [
{
// Get the sub verifier name from the Web3Auth Dashboard
verifier: "YOUR_SUB_VERIFIER_NAME",
// Pass the JWT token
idToken: "YOUR_JWT_TOKEN",
},
],
});
Get a native provider
The method returns the provider instance that can be used to interact with different blockchain
networks. Please note, if there's no active session, the method will return null
.
Please refer to the provider documentation for more details.
Usage
const provider = web3auth.provider;
// Use the provider to interact with the blockchain
Get sessionId
The method returns the session id for the current active session as the string.
Usage
const sessionId = web3auth.sessionId;
Authenticate the user
The method authenticates the connected user, and returns user auth info containing the Web3Auth JWT token. You can use the idToken for backend verification.
Usage
const userAuthInfo = await web3auth.authenticateUser();
Response
export type UserAuthInfo = {
idToken: string;
};
Add a new chain
To add a new chain to your Web3Auth provider instance you can use the addChain
method.
Parameters
The method accepts a CustomChainConfig
object as an input.
- Table
- Type Declarations
Name | Description |
---|---|
chainNamespace | Namespace of the chain. It ChainNamespaceType type. |
chainId | The chain id of the network in Hex format. |
rpcTarget | RPC target URL for the chain. The RPC url is used to interact with the blockchain network. |
wsTarget? | Web socket target URL for the chain. |
displayName? | Display Name for the chain. |
blockExplorerUrl? | Blockchain explorer URL for the network. |
ticker? | Network's native currency ticker (e.g: ETH for Ethereum) |
tickerName? | Network's native currency name (e.g: Ethereum ). |
decimals? | Network's native currency decimal precision (e.g: 18 for Ethereum). Default value is 18. |
logo? | Logo for the token. |
isTestnet? | Whether the network is testnet or not. |
export declare const ADAPTER_NAMESPACES: {
readonly EIP155: "eip155";
readonly SOLANA: "solana";
readonly CASPER: "casper";
readonly XRPL: "xrpl";
readonly MULTICHAIN: "multichain";
};
declare type ChainNamespaceType = (typeof CHAIN_NAMESPACES)[keyof typeof CHAIN_NAMESPACES];
export type CustomChainConfig = {
chainNamespace: ChainNamespaceType;
/**
* The chain id of the chain
*/
chainId: string;
/**
* RPC target Url for the chain
*/
rpcTarget: string;
/**
* web socket target Url for the chain
*/
wsTarget?: string;
/**
* Display Name for the chain
*/
displayName?: string;
/**
* Url of the block explorer
*/
blockExplorerUrl?: string;
/**
* Default currency ticker of the network (e.g: ETH)
*/
ticker?: string;
/**
* Name for currency ticker (e.g: `Ethereum`)
*/
tickerName?: string;
/**
* Number of decimals for the currency ticker (e.g: 18)
*/
decimals?: number;
/**
* Logo for the token
*/
logo?: string;
/**
* Whether the network is testnet or not
*/
isTestnet?: boolean;
};
Switch the chain
To switch the chain for the provider instance you need to call the switchChain
method. Please make
sure, you have first added the chain using the addChain method.
Parameters
Name | Description |
---|---|
chainId | The hex chain ID of the blockchain network you want to switch to |
Usage
// Switches the chain to the Polygon network
await web3auth.switchChain({ chainId: "0x89" });
Logging out the user
To logout the user and clear the session, call the logout
method. Please note, this method only
clears the Web3Auth session, and doesn't clears the OAuth session.
Usage
await web3auth.logout();
Wallet Services Plugin Methods
You can use the Wallet Services Plugin to enable additional functionalities like showing the Wallet UI Screen, Wallet Connect Scanner, and initiating topup for the user.
Learn more about the Wallet Services Plugin in the Wallet Services SDK Reference.
Show WalletConnect Scanner
You can use the showWalletConnectScanner
function to show the Wallet Connect Scanner, and connect
with dApps having Wallet Connect login option. This is useful for interoperability with dApps having
Wallet Connect login option.
Learn more about showWalletConnectScanner
.
Fiat On Ramp
You can use the showCheckout
function to show the TopUp modal, allowing users to select their
local currency and specify the token to top up their wallet.
Learn more about showCheckout
.
Embedded Wallet UI
You can use the showWalletUI
function to show the templated wallet UI services.
Learn more about showWalletUI
.
Transaction Confirmatons Screen
You can use the wallet services provider to integrate prebuilt transaction confirmation screens into your application. Upon successful completion, you can retrieve the signature for the request. Learn more about transaction confirmation screens.