From 1fb3fcb1e65444f3293e236c86ba6e34d4e1ce87 Mon Sep 17 00:00:00 2001 From: yaso Date: Tue, 14 Jan 2025 12:56:38 +0200 Subject: [PATCH] add Biometric screen --- .../authentication/biometric_check.dart | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 Frontend/lib/mih_packages/authentication/biometric_check.dart diff --git a/Frontend/lib/mih_packages/authentication/biometric_check.dart b/Frontend/lib/mih_packages/authentication/biometric_check.dart new file mode 100644 index 00000000..c49a5bef --- /dev/null +++ b/Frontend/lib/mih_packages/authentication/biometric_check.dart @@ -0,0 +1,205 @@ +import 'package:Mzansi_Innovation_Hub/main.dart'; +import 'package:Mzansi_Innovation_Hub/mih_components/mih_inputs_and_buttons/mih_button.dart'; +import 'package:Mzansi_Innovation_Hub/mih_components/mih_layout/mih_action.dart'; +import 'package:Mzansi_Innovation_Hub/mih_components/mih_layout/mih_body.dart'; +import 'package:Mzansi_Innovation_Hub/mih_components/mih_layout/mih_header.dart'; +import 'package:Mzansi_Innovation_Hub/mih_components/mih_layout/mih_layout_builder.dart'; +import 'package:Mzansi_Innovation_Hub/mih_packages/mih_home/mih_profile_getter.dart'; +import 'package:flutter/material.dart'; +import 'package:local_auth/local_auth.dart'; +import 'package:supertokens_flutter/supertokens.dart'; + +class BiometricCheck extends StatefulWidget { + final bool personalSelected; + final bool firstBoot; + const BiometricCheck({ + super.key, + required this.personalSelected, + required this.firstBoot, + }); + + @override + State createState() => _BiometricCheckState(); +} + +class _BiometricCheckState extends State { + bool _isBioAuthenticated = false; + final LocalAuthentication _auth = LocalAuthentication(); + + MIHAction getActionButton() { + return MIHAction( + icon: Padding( + padding: const EdgeInsets.all(10.0), + child: SizedBox( + height: 50, + child: Image.asset('images/logo_light.png'), + ), + ), + iconSize: 35, + onTap: () async { + await SuperTokens.signOut(completionHandler: (error) { + print(error); + }); + if (await SuperTokens.doesSessionExist() == false) { + Navigator.of(context).popAndPushNamed('/'); + } + // Navigator.of(context).pushNamed( + // '/about', + // //arguments: widget.signedInUser, + // ); + }, + ); + } + + MIHHeader getHeader() { + return const MIHHeader( + headerAlignment: MainAxisAlignment.center, + headerItems: [ + Text( + "", + style: TextStyle( + fontWeight: FontWeight.bold, + fontSize: 25, + ), + ), + ], + ); + } + + void authenticateUser() async { + final bool canAuthWithBio = await _auth.canCheckBiometrics; + print("Biomentric Available: $canAuthWithBio"); + if (canAuthWithBio) { + try { + final bool didBioAuth = await _auth.authenticate( + localizedReason: "Authenticate to access MIH", + options: const AuthenticationOptions( + biometricOnly: false, + ), + ); + if (didBioAuth) { + setState(() { + _isBioAuthenticated = true; + }); + } + print("Authenticated: $didBioAuth"); + } catch (error) { + print(error); + } + } + } + + MIHBody getBody() { + return MIHBody( + borderOn: false, + bodyItems: [ + SafeArea( + child: Center( + child: Padding( + padding: const EdgeInsets.all(25.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + //logo + Icon( + Icons.fingerprint, + size: 100, + color: + MzanziInnovationHub.of(context)!.theme.secondaryColor(), + ), + //spacer + const SizedBox(height: 10), + //Heading + Text( + 'Biomentric Authentication', + style: TextStyle( + fontSize: 25, + fontWeight: FontWeight.bold, + color: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + ), + ), + //spacer + const SizedBox(height: 25), + if (!_isBioAuthenticated) + Icon( + Icons.lock, + size: 200, + color: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + ), + if (_isBioAuthenticated) + Icon( + Icons.lock_open, + size: 200, + color: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + ), + const SizedBox(height: 30), + Container( + alignment: Alignment.bottomCenter, + child: SizedBox( + width: 500.0, + height: 50.0, + child: MIHButton( + buttonText: "Unlock", + buttonColor: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + textColor: MzanziInnovationHub.of(context)! + .theme + .primaryColor(), + onTap: () async { + //Check Biometrics + authenticateUser(); + }, + ), + ), + ), + ], + ), + ), + ), + ), + ], + ); + } + + Widget getBiomentricAuthScreen() { + return MIHLayoutBuilder( + actionButton: getActionButton(), + header: getHeader(), + secondaryActionButton: null, + body: getBody(), + actionDrawer: null, + secondaryActionDrawer: null, + bottomNavBar: null, + pullDownToRefresh: false, + onPullDown: () async {}, + ); + } + + @override + Widget build(BuildContext context) { + if (MzanziInnovationHub.of(context)!.theme.getPlatform() == "Web") { + return MIHProfileGetter( + personalSelected: widget.personalSelected, + ); + } else if (!widget.firstBoot) { + return MIHProfileGetter( + personalSelected: widget.personalSelected, + ); + } else { + if (_isBioAuthenticated) { + return MIHProfileGetter( + personalSelected: widget.personalSelected, + ); + } else { + return getBiomentricAuthScreen(); + } + } + } +}