77 lines
2.6 KiB
Dart
77 lines
2.6 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:local_auth/local_auth.dart';
|
|
import 'package:tanami_app/core/utils/secure/secure_storage_service.dart';
|
|
import 'package:tanami_app/features/biometric/domain/repository/biometric_api.dart';
|
|
|
|
import '../../../../Api_Helper/base_manager.dart';
|
|
import 'biometric_event.dart';
|
|
import 'biometric_state.dart';
|
|
|
|
class BiometricBloc extends Bloc<BiometricEvent, BiometricState> {
|
|
final LocalAuthentication localAuthentication;
|
|
final SecureStorageService secureStorageService;
|
|
|
|
BiometricBloc(this.localAuthentication, this.secureStorageService)
|
|
: super(BiometricInitial()) {
|
|
on<CheckBiometricEvent>(_onCheckBiometric);
|
|
on<AuthenticateBiometricEvent>(_onAuthenticateBiometric);
|
|
}
|
|
|
|
Future<void> _onCheckBiometric(
|
|
CheckBiometricEvent event, Emitter<BiometricState> emit) async {
|
|
emit(BiometricChecking());
|
|
bool canAuthenticate = await localAuthentication.canCheckBiometrics;
|
|
emit(BiometricChecked(canAuthenticate));
|
|
}
|
|
|
|
Future<void> _onAuthenticateBiometric(
|
|
AuthenticateBiometricEvent event, Emitter<BiometricState> emit) async {
|
|
emit(BiometricAuthenticating());
|
|
try {
|
|
bool authenticated = false;
|
|
if (Platform.isIOS) {
|
|
authenticated = await localAuthentication.authenticate(
|
|
localizedReason: 'Please authenticate to access the app',
|
|
options: const AuthenticationOptions(
|
|
useErrorDialogs: true,
|
|
stickyAuth: true,
|
|
biometricOnly: true,
|
|
),
|
|
);
|
|
} else if (Platform.isAndroid) {
|
|
authenticated = await localAuthentication.authenticate(
|
|
localizedReason: 'Please authenticate to access the app',
|
|
options: const AuthenticationOptions(
|
|
useErrorDialogs: true,
|
|
stickyAuth: true,
|
|
),
|
|
);
|
|
}
|
|
|
|
if (authenticated) {
|
|
Map<String, dynamic> biometricLoginData = {
|
|
"token": await secureStorageService.read("temp_token"),
|
|
};
|
|
|
|
ResponseData response =
|
|
await BiometricAPIServices().biometricLoginApi(biometricLoginData);
|
|
if (response.status == ResponseStatus.SUCCESS) {
|
|
await secureStorageService.write(
|
|
'accesstoken', response.data["data"]["accessToken"]);
|
|
await secureStorageService.write(
|
|
'refreshtoken', response.data["data"]["refreshToken"]);
|
|
emit(BiometricAuthenticated());
|
|
} else {
|
|
emit(BiometricFailed('Authentication failed'));
|
|
}
|
|
} else {
|
|
emit(BiometricFailed('Authentication failed'));
|
|
}
|
|
} catch (e) {
|
|
emit(BiometricFailed(e.toString()));
|
|
}
|
|
}
|
|
}
|