Skip to main content

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.

ParameterDescription
networkThe 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
clientIdThe clientId for your Web3Auth project. You can get it from Web3Auth dashboard.
sessionTimeSpecifies the session duration in seconds. By default, the value is set to 86400 seconds (1 day), with a maximum session duration of up to 7 days.
serverTimeOffsetSpecify a custom server time offset. The default value is 0.
storageServerUrl?Specifies the storage server URL. The default value is https://session.web3auth.io.

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. We have included Session Management in this SDK, so you can use the method to get the SessionData without re-logging in the user. If a user has an active session, it will return the SessionData, otherwise, it will throw an error for inactive session.

import android.content.Context

val context: Context = "YOUR_APPLICATION_CONTEXT"
val sessionDataCF = singleFactorAuth.initialize(context)

sessionDataCF.whenComplete {sessionData, error ->
if(error != null) {
// Session is no longer valid, or something went wrong
// Initiate the login flow again
} else if(sessionData == null) {
// No active session found
} else {
// Session is present
// You can use the sessionData to get the public address, private key,
// userInfo, and signatures.
Log.i("Public address", sessionData.publicAddress)
}
}