Initializing Single Factor Auth Android SDK
Once you have installed the Web3Auth SDK, the next crucial step is to initialize it. This step requires passing various parameters that align with your project preferences. It's important to note that the initialization process is critical to the successful use of Web3Auth.
Parameters
In your activity, create a SingleFactorAuth
instance with Web3AuthOptions
. You can define the
Web3Auth network, client id, and other parameters during initialization.
Parameter | Description |
---|---|
network | The Web3auth network is to be used by the SDK. Supported values are Web3AuthNetwork.SAPPHIRE_MAINNET , Web3AuthNetwork.SAPPHIRE_DEVNET ,Web3AuthNetwork.MAINNET , Web3AuthNetwork.TESTNET , Web3AuthNetwork.CYAN , and Web3AuthNetwork.AQUA |
clientId | The clientId for your Web3Auth project. You can get it from Web3Auth dashboard. |
sessionTime | Specifies the session duration in seconds. By default, the value is set to 86400 seconds (1 day), with a maximum session duration of up to 30 days. |
serverTimeOffset | Specify a custom server time offset. The default value is 0. |
storageServerUrl? | Specifies the storage server URL. The default value is https://session.web3auth.io . |
whiteLabel? | You can pass the white labeling options for web3auth. It helps you define your brand app's custom UI, and branding for the Wallet Services feature. The recommended way to configure the whiteLabel is through the Web3Auth Dashboard. Learn how to configure the whiteLabel via Web3Auth Dashboard. |
redirectUri? | URL that Web3Auth will redirect API responses upon successful request method call. Please note, that it's mandatory to configure the redirectUri if you are using the request method. |
Create Instance
import android.content.Context
import com.web3auth.singlefactorauth.SingleFactorAuth
import com.web3auth.singlefactorauth.types.Web3AuthOptions
import org.torusresearch.fetchnodedetails.types.Web3AuthNetwork
// You can get the client id for your Web3Auth project from Web3Auth dashboard.
val web3AuthOptions = Web3AuthOptions(
"YOUR_WEB3AUTH_CLIENT_ID",
Web3AuthNetwork.SAPPHIRE_MAINNET
)
val context: Context = "YOUR_APPLICATION_CONTEXT"
val singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
Initialize
To initialize the SDK, you can use the initialize
method. This method helps you initialize the SDK
with existing session. After successful initialization, you can use the
getSessionData method to check if the user is logged in or not.
import android.content.Context
val context: Context = "YOUR_APPLICATION_CONTEXT"
val sessionDataCF = singleFactorAuth.initialize(context)
sessionDataCF.whenComplete {sessionData, error ->
if(error != null) {
// Something went wrong
// Initiate the login flow again
} else {
// You can use the SessionData to check if the user is
//logged in or not
}
}