Using Single Factor Auth Flutter SDK
After successfully installing and initializing SingleFactorAuth, you can use it to authenticate your users and obtain their private and public keys.
Web3Auth SFA Flutter SDK only works for users who have not enabled MFA. For MFA enabled users, you'll see an Error message.
Functionality Overview
The SingleFactorAuth instance natively provides the following methods:
Method | Description |
---|---|
connect | Use to login user and retrieve private key pair. |
logout | Use to logout existing user. |
connected | Use to check whether the user is logged in or not. |
getSessionData | This method helps to get the session data for valid session. |
Login User
Please refer to the Authentication section for more details on the setting up your verifier and other authentication parameters.
To obtain a user's private key using the Web3Auth SFA Flutter SDK, you can call the connect
method. The method accepts LoginParams
, and returns SFAKey
.
Parameters
- Table
- Class
Parameter | Description |
---|---|
verifier | The verifier parameter takes the name of the custom verifier from the Web3Auth Dashboard. This is a required field that must be a String . If you're using an aggregate verifier, make sure to pass the aggregate verifier name. |
verifierId | The verifierID takes the JWT verifier ID to be used for JWT/ID token verification. It can be an email, sub, or custom value available in the JWT token. |
idToken | The idToken accepts a JWT token obtained from the user's login provider. |
subVerifierInfoArray? | Sub verifier info. Usually used during the aggregate verifier. It takes List<TorusSubVerifierInfo> as a value. |
class LoginParams {
final String verifier;
final String verifierId;
final String idToken;
final String? aggregateVerifier;
LoginParams({
required this.verifier,
required this.verifierId,
required this.idToken,
this.aggregateVerifier,
});
Map<String, dynamic> toJson() {
return {
'verifier': verifier,
'verifierId': verifierId,
'idToken': idToken,
'aggregateVerifier': aggregateVerifier,
};
}
}
Usage
- Single verifier
- Aggregate verifier
final loginParams = LoginParams(
verifier: 'YOUR_VERIFIER_NAME',
verifierId: 'YOUR_VERIFIER_ID',
idToken: 'YOUR_ID_TOKEN',
);
try { final sessionData = await singleFactorAuthFlutter.connect(loginParams); } catch (e) { //
Handle error }
final subVerifierInfoArray = [
TorusSubVerifierInfo(
'YOUR_SUB_VERIFIER_NAME',
'YOUR_ID_TOKEN',
),
];
final loginParams = LoginParams(
verifier: 'YOUR_AGGREGATE_VERIFIER_NAME',
verifierId: 'YOUR_VERIFIER_ID',
idToken: 'YOUR_ID_TOKEN',
subVerifierInfoArray: subVerifierInfoArray,
);
try {
final sessionData = await singleFactorAuthFlutter.connect(loginParams);
} catch (e) {
// Handle error
}
Logout User
To logout the current user, you can use the logout
method. Please note, the method will not logout
the user from the authentication provider, it'll only logout and invalidate the Web3Auth session.
Usage
try {
await singleFactorAuthFlutter.logout();
// Logged out successfully
} catch (e) {
// Handle error
}
Check User's Logged In Status
You can use the connected
method to check whether the user is logged in Web3Auth or not. Please
note, you should call this method after the initialize
method if you want to check the user's
connection status for an existing session.
Usage
final isConnected = await singleFactorAuthFlutter.connected();
Get Session Data
We have included Session Management in this SDK, so calling the getSessionData
will retrive the
user's SessionData
without re-logging in the user if a user has an active session. Otherwise, it
will return null
.
Please note, you should use this method after the initialize
method.
Usage
final SessionData? sessionData = await singleFactorAuthFlutter.getSessionData();
if (sessionData != null) {
// User is logged in
} else {
// User is not logged in
}
Response
The SessionData
has the following properties to retrive the relevant session information.
Name | Description |
---|---|
privateKey | Retrieves the user's private key. |
publicAddress | Retrieves the user's public address. |
userInfo? | Retrieves the user's information like email, name, verifier id, and more. |
signatures? | Retrieves the node's signatures that are returned for request. |