Initializing Single Factor Auth Flutter SDK
After installation, the next step to use Web3Auth SFA Flutter is to initialize the SDK and is
achieved by init
method. 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 SFA Flutter.
Parameters
The init
method requires a Web3AuthOptions
object. Please note that these are the important
parameters to configure the SDK.
- Table
- Class
Parameter | Description |
---|---|
network | The Web3auth network 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 client id 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. |
class Web3AuthOptions {
final Web3AuthNetwork network;
final String clientId;
final int sessionTime;
Web3AuthOptions(
{required this.network,
required this.clientId,
this.sessionTime = 86400});
Map<String, dynamic> toJson() {
return {
'network': network.name,
'clientId': clientId,
'sessionTime': sessionTime,
};
}
}
enum Web3AuthNetwork {
mainnet,
testnet,
cyan,
aqua,
celeste,
sapphire_testnet,
sapphire_mainnet
}
Initialize Web3AuthFlutter
To initialize the SDK, you can use the init
method. Once, the SDK is initialized, you can use the
initialize
method to 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.
Please note, the initialize
method will throw an error if there is no active session present. You
can swallow the error or handle it gracefully in your app.
import 'package:single_factor_auth_flutter/single_factor_auth_flutter.dart';
import 'package:single_factor_auth_flutter/enums.dart';
import 'package:single_factor_auth_flutter/input.dart';
final web3authOptions = Web3AuthOptions(
network: Web3AuthNetwork.sapphire_devnet,
clientId: "YOUR_WEB3AUTH_CLIENT_ID",
);
final singleFactorAuthFlutter = SingleFactorAuthFlutter();
await singleFactorAuthFlutter.init(
web3authOptions,
);
try {
await singleFactorAuthFlutter.initialize();
} catch (e) {
// SDK will throw an error if there is no active session
// present in the storage.
//
// Handle the error gracefully in your app.
}