Backup data is no longer the recommended approach to perform account recovery. The recommended flow is using the temporary state. Follow the guide on account recovery.
Back up your user's data so you can restore their Keyless account when necessary.
Create a backup (not recommended)
Create backups after authentication, and push them into your backup system. Each backup is encrypted with aes-gcm using the backupKey as symmetric key. You need both backupData and backupKey to restore an account with Keyless.
val configuration = AuthenticationConfiguration.builder
.retrievingBackup()
.build()
Keyless.authenticate(
authenticationConfiguration = configuration,
onCompletion = { authResult ->
when (authResult) {
is Keyless.KeylessResult.Success -> {
// use the backup
val backup: KeylessBackup? = authResult.value.backup
}
is Keyless.KeylessResult.Failure -> Log.d("KeylessSDK ", "Authentication failure - error code ${result.error.code}")
}
}
)
let authenticationConfiguration = Keyless.AuthenticationConfiguration.builder
.retrievingBackup()
.build()
Keyless.authenticate(authenticationConfiguration: authenticationConfiguration) { result in
switch result {
case .success(let authenticationSuccess):
// Handle your backup system logic
print("Retrieve data for backup successful")
print("Backup data: \(authenticationSuccess.backup?.data)")
print("Backup key: \(authenticationSuccess.backup?.key)")
case .failure(let error):
// Handle error and display an error to the user.
print("Retrieve data for backup failed with error: \(error.message)")
}
}
import 'package:keyless_flutter_sdk/keyless.dart';
import 'package:keyless_flutter_sdk/models/configurations/authentication_configuration.dart';
// Retrieve temporary state during authentication
final configuration = BiomAuthConfig(shouldRetrieveTemporaryState: true);
try {
final result = await Keyless.instance.authenticate(configuration);
if (result.temporaryState != null) {
print("Temporary state retrieved successfully");
// Store temporaryState securely
}
} catch (error) {
print("Failed to retrieve temporary state: $error");
}
Restore from a backup (not recommended)
Restore the user account by providing backupData and backupKey to the withBackup method:
val configuration = EnrollmentConfiguration.builder
.withBackup(
backupKey = backupKey,
backupData = backupData
)
.build()
Keyless.enroll(
enrollmentConfiguration = configuration,
onCompletion = { result ->
when (result) {
is Keyless.KeylessResult.Success -> Log.d("KeylessSDK ", "Enroll success - userId ${result.value.keylessId}")
is Keyless.KeylessResult.Failure -> Log.d("KeylessSDK ", "Enroll failure - error code ${result.error.code}")
}
})
let enrollmentConfiguration = Keyless.EnrollmentConfiguration.builder
.withBackup(Keyless.Backup(data: backupData, key: backupKey))
.build()
Keyless.enroll(enrollmentConfiguration: enrollmentConfiguration) { result in
switch result {
case .success(let enrollmentSuccess):
print("Enrollment from backup finished successfully")
case .failure(let error):
print("Enrollment from backup finished with error: \(error.message)")
}
}
import 'package:keyless_flutter_sdk/keyless.dart';
import 'package:keyless_flutter_sdk/models/configurations/enrollment_configuration.dart';
// Create enrollment configuration with backup
final configuration = BiomEnrollConfig(
backup: KeylessBackup(
data: backupData,
key: backupKey
)
);
try {
final result = await Keyless.instance.enroll(configuration);
print("Enrollment from backup successful");
} catch (error) {
print("Enrollment from backup failed: $error");
}