api integration
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
abstract class BiometricEvent {}
|
||||
|
||||
class CheckBiometricEvent extends BiometricEvent {}
|
||||
|
||||
class AuthenticateBiometricEvent extends BiometricEvent {}
|
||||
@@ -1,21 +0,0 @@
|
||||
abstract class BiometricState {}
|
||||
|
||||
class BiometricInitial extends BiometricState {}
|
||||
|
||||
class BiometricChecking extends BiometricState {}
|
||||
|
||||
class BiometricChecked extends BiometricState {
|
||||
final bool canAuthenticate;
|
||||
|
||||
BiometricChecked(this.canAuthenticate);
|
||||
}
|
||||
|
||||
class BiometricAuthenticating extends BiometricState {}
|
||||
|
||||
class BiometricAuthenticated extends BiometricState {}
|
||||
|
||||
class BiometricFailed extends BiometricState {
|
||||
final String message;
|
||||
|
||||
BiometricFailed(this.message);
|
||||
}
|
||||
@@ -10,9 +10,9 @@ import '../../../../core/routes/route_name.dart';
|
||||
import '../../../../core/routes/routes.dart';
|
||||
import '../../../../core/styles/app_images.dart';
|
||||
import '../../../../shared/components/device_locked_dialog.dart';
|
||||
import '../bloc/biometric_bloc.dart';
|
||||
import '../bloc/biometric_event.dart';
|
||||
import '../bloc/biometric_state.dart';
|
||||
import '../../bloc/biometric_bloc.dart';
|
||||
import '../../bloc/biometric_event.dart';
|
||||
import '../../bloc/biometric_state.dart';
|
||||
|
||||
class BiometricLayout extends StatelessWidget {
|
||||
const BiometricLayout({super.key});
|
||||
|
||||
Reference in New Issue
Block a user