diff --git a/lib/customAppBar/enrollmentAppBar.dart b/lib/customAppBar/enrollmentAppBar.dart index adfc905..78b663f 100644 --- a/lib/customAppBar/enrollmentAppBar.dart +++ b/lib/customAppBar/enrollmentAppBar.dart @@ -25,7 +25,7 @@ class _CustomAppBarState extends State { final String? token = prefs.getString('token'); if (token != null && token.isNotEmpty) { - // await prefs.clear(); + await prefs.clear(); Navigator.pushNamed(context, 'login'); } } diff --git a/lib/main.dart b/lib/main.dart index 14ccb55..e326504 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,3 @@ -import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:nhance_app_pwa/pages/enrollment/addons.dart'; @@ -28,7 +27,6 @@ import 'models/environment.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); - await Firebase.initializeApp(); await dotenv.load(fileName: Environment.fileName); final AuthService _authService = AuthService(); diff --git a/lib/models/platform_helper_mobile.dart b/lib/models/platform_helper_mobile.dart new file mode 100644 index 0000000..1a917e8 --- /dev/null +++ b/lib/models/platform_helper_mobile.dart @@ -0,0 +1,5 @@ +import 'dart:io'; + +bool isMobilePlatform() { + return Platform.isAndroid || Platform.isIOS; +} diff --git a/lib/models/platform_helper_other.dart b/lib/models/platform_helper_other.dart new file mode 100644 index 0000000..4c467d0 --- /dev/null +++ b/lib/models/platform_helper_other.dart @@ -0,0 +1,3 @@ +bool isMobilePlatform() { + return false; +} diff --git a/lib/pages/login.dart b/lib/pages/login.dart index 337d9bf..142998d 100644 --- a/lib/pages/login.dart +++ b/lib/pages/login.dart @@ -2,16 +2,15 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:http/http.dart' as http; -import 'package:nhance_app_pwa/pages/session/SetPinBiometric.dart'; import 'dart:convert'; import 'dart:io'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:flutter_animated_button/flutter_animated_button.dart'; import 'package:google_fonts/google_fonts.dart'; - import '../customAppBar/responsive.dart'; import '../customAppBar/toastHelper.dart'; import '../models/environment.dart'; +import '../models/platform_helper_mobile.dart' + if (dart.library.html) '../models/platform_helper_other.dart'; class login extends StatefulWidget { const login({Key? key}); @@ -31,30 +30,16 @@ class _loginState extends State { void initState() { countryController.text = "+91"; super.initState(); - checkLoginPin(); - WidgetsBinding.instance - .addPostFrameCallback((_) => checkTokenAvailability()); + checkLoginPinOnMobile(); } - Future checkTokenAvailability() async { - if (_isCheckingToken) return; - _isCheckingToken = true; - print('checkTokenAvailability'); - final SharedPreferences prefs = await SharedPreferences.getInstance(); - final String? token = prefs.getString('token'); - final mpin = prefs.getString('user_pin'); - int skipStatus = prefs.getInt('skipStatus') ?? 1; - final emp_status = prefs.getString('emp_status'); - - // if (prefs.containsKey('skipStatus') && skipStatus == 1) { - // if (prefs.containsKey('user_pin') && mpin != null && mpin.isNotEmpty) { - // Navigator.pushReplacementNamed(context, 'pinPage'); - // } - // } - // checkLoginPin(); + void checkLoginPinOnMobile() { + if (isMobilePlatform()) { + checkLoginPin(context); + } } - Future checkLoginPin() async { + Future checkLoginPin(BuildContext context) async { print('checkLoginPin'); try { final SharedPreferences prefs = await SharedPreferences.getInstance(); @@ -73,7 +58,8 @@ class _loginState extends State { Map data = json.decode(response.body); if (data['status'] == 'success') { print('data'); - prefs.setString('mpin', data['data']); + prefs.setString('mpinText', data['data']); + prefs.setString('mpin', data['Mpin']); Navigator.pushReplacementNamed(context, 'pinPage'); } } else { diff --git a/lib/pages/postEnrollment/profile.dart b/lib/pages/postEnrollment/profile.dart index 5470057..6417010 100644 --- a/lib/pages/postEnrollment/profile.dart +++ b/lib/pages/postEnrollment/profile.dart @@ -11,6 +11,10 @@ import '../../customAppBar/responsive.dart'; import '../../customAppBar/tabs.dart'; import '../../customAppBar/toastHelper.dart'; import 'package:http/http.dart' as http; +import '../../models/platform_helper_mobile.dart' + if (dart.library.html) '../../models/platform_helper_other.dart'; + +import '../session/authenticationService.dart'; class profile extends StatefulWidget { const profile({Key? key}) : super(key: key); @@ -21,6 +25,7 @@ class profile extends StatefulWidget { class _profileState extends State { late ApiService apiService; + final AuthService _authService = AuthService(); int _currentIndex = 0; bool isActive = true; dynamic _token; @@ -140,12 +145,25 @@ class _profileState extends State { } Future logout(BuildContext context) async { - final prefs = await SharedPreferences.getInstance(); - if (Responsive.isDesktop(context)) { + if (isMobilePlatform()) { + final SharedPreferences prefs = await SharedPreferences.getInstance(); + + // Get the value of the mobile_number key + String? mobileNumber = prefs.getString('empMobileNo'); + + // Clear all keys await prefs.clear(); + + // Re-set the mobile_number key + if (mobileNumber != null) { + await prefs.setString('empMobileNo', mobileNumber); + } + ToastHelper.showSuccessToast(context, 'logout'); + // Navigate to login page Navigator.pushNamed(context, 'login'); } else { - // await prefs.clear(); + final prefs = await SharedPreferences.getInstance(); + await prefs.clear(); Navigator.pushNamed(context, 'login'); } } diff --git a/lib/pages/session/SetPinBiometric.dart b/lib/pages/session/SetPinBiometric.dart index 131dec5..844de85 100644 --- a/lib/pages/session/SetPinBiometric.dart +++ b/lib/pages/session/SetPinBiometric.dart @@ -35,6 +35,7 @@ class _pinPageState extends State { dynamic clientName; dynamic clientLogo; dynamic emp_status; + dynamic bioMetricMpin; @override void initState() { @@ -51,15 +52,17 @@ class _pinPageState extends State { } Future _checkBiometricAuthentication() async { + final SharedPreferences prefs = await SharedPreferences.getInstance(); + bioMetricMpin = prefs.getString('mpin'); bool authenticated = await _authService.authenticateWithBiometrics(); if (authenticated) { - enterPinApi(); + enterPinApi(bioMetricMpin); } else { ToastHelper.showErrorToast(context, 'Biometric authentication failed'); } } - Future enterPinApi() async { + Future enterPinApi(mpin) async { try { final SharedPreferences prefs = await SharedPreferences.getInstance(); empMobileNo = prefs.getString('empMobileNo'); @@ -67,8 +70,7 @@ class _pinPageState extends State { final response = await http.post( Uri.parse(Environment.apiUrl + 'verifyMpin'), - body: json.encode( - {'mobile_number': empMobileNo, 'mpin': _pinController.text}), + body: json.encode({'mobile_number': empMobileNo, 'mpin': mpin}), headers: { HttpHeaders.contentTypeHeader: 'application/json', }, @@ -124,7 +126,7 @@ class _pinPageState extends State { Future enterLoginPin() async { try { if (_formKey.currentState!.validate()) { - enterPinApi(); + enterPinApi(_pinController.text); } } catch (e) { ToastHelper.showErrorToast(context, 'Something went wrong'); @@ -573,51 +575,4 @@ class _pinPageState extends State { ]), ))); } - // Widget build(BuildContext context) { - // return Scaffold( - // appBar: AppBar(title: Text('Authenticate')), - // body: Padding( - // padding: const EdgeInsets.all(16.0), - // child: Column( - // children: [ - // ElevatedButton( - // onPressed: () async { - // bool authenticated = - // await _authService.authenticateWithBiometrics(); - // if (authenticated) { - // Navigator.pushReplacementNamed(context, 'home'); - // } else { - // ScaffoldMessenger.of(context).showSnackBar(SnackBar( - // content: Text('Biometric authentication failed'), - // )); - // } - // }, - // child: Text('Authenticate with Biometric'), - // ), - // TextField( - // controller: _pinController, - // decoration: InputDecoration(labelText: 'Enter PIN'), - // keyboardType: TextInputType.number, - // obscureText: true, - // ), - // SizedBox(height: 20), - // ElevatedButton( - // onPressed: () async { - // String pin = _pinController.text; - // String? savedPin = await _authService.getPin(); - // if (pin == savedPin) { - // Navigator.pushReplacementNamed(context, 'home'); - // } else { - // ScaffoldMessenger.of(context).showSnackBar(SnackBar( - // content: Text('Incorrect PIN'), - // )); - // } - // }, - // child: Text('Authenticate with PIN'), - // ), - // ], - // ), - // ), - // ); - // } } diff --git a/lib/pages/session/settingUpPinAndBiometric.dart b/lib/pages/session/settingUpPinAndBiometric.dart index 8128e07..b6bf85e 100644 --- a/lib/pages/session/settingUpPinAndBiometric.dart +++ b/lib/pages/session/settingUpPinAndBiometric.dart @@ -158,19 +158,6 @@ class _pinSettingPageState extends State { top: 0, // Example value for mobile ); } - // final defaultPinTheme = PinTheme( - // width: 56, - // height: 56, - // textStyle: TextStyle( - // fontSize: 20, - // color: Color.fromRGBO(30, 60, 87, 1), - // fontWeight: FontWeight.w600, - // ), - // decoration: BoxDecoration( - // border: Border.all(color: Color.fromRGBO(234, 239, 243, 1)), - // borderRadius: BorderRadius.circular(20), - // ), - // ); final defaultPinTheme = PinTheme( width: 56, @@ -664,41 +651,4 @@ class _pinSettingPageState extends State { ]), ))); } - - // @override - // Widget build(BuildContext context) { - // return Scaffold( - // appBar: AppBar(title: Text('Set PIN and Biometric')), - // body: Padding( - // padding: const EdgeInsets.all(16.0), - // child: Column( - // children: [ - // TextField( - // controller: _pinController, - // decoration: InputDecoration(labelText: 'Enter PIN'), - // keyboardType: TextInputType.number, - // obscureText: true, - // ), - // SizedBox(height: 20), - // ElevatedButton( - // onPressed: () async { - // String pin = _pinController.text; - // await _authService.savePin(pin); - // bool biometricEnabled = - // await _authService.authenticateWithBiometrics(); - // if (biometricEnabled) { - // Navigator.pushReplacementNamed(context, 'home'); - // } else { - // ScaffoldMessenger.of(context).showSnackBar(SnackBar( - // content: Text('Biometric authentication failed'), - // )); - // } - // }, - // child: Text('Set PIN and Enable Biometric'), - // ), - // ], - // ), - // ), - // ); - // } } diff --git a/lib/pages/verify.dart b/lib/pages/verify.dart index 3d33250..a415cab 100644 --- a/lib/pages/verify.dart +++ b/lib/pages/verify.dart @@ -12,6 +12,8 @@ import 'package:jwt_decode/jwt_decode.dart'; import '../customAppBar/responsive.dart'; import '../customAppBar/toastHelper.dart'; import '../models/environment.dart'; +import '../models/platform_helper_mobile.dart' + if (dart.library.html) '../models/platform_helper_other.dart'; class MyVerify extends StatefulWidget { const MyVerify({Key? key}) : super(key: key); @@ -157,9 +159,15 @@ class _MyVerifyState extends State { // Redirect to another page final token = prefs.getString('token'); // int skipStatus = prefs.getInt('skipStatus') ?? 1; - final mpin = prefs.getString('mpin'); - - if (prefs.containsKey('mpin') && mpin != 'Mpin - exist') { + // final mpinText = prefs.getString('mpinText'); + print(isMobilePlatform()); + if (isMobilePlatform()) { + // if (prefs.containsKey('mpin') && mpinText == 'Mpin - exist') { + if (token != null && token.isNotEmpty) { + Navigator.pushReplacementNamed(context, 'pinSettingPage'); + } + // } + } else { if (token != null && token.isNotEmpty) { ToastHelper.showSuccessToast(context, 'Successfully Login'); if (emp_status == 'enrolled') { @@ -168,8 +176,6 @@ class _MyVerifyState extends State { Navigator.pushReplacementNamed(context, 'empDetails'); } } - } else { - Navigator.pushReplacementNamed(context, 'pinSettingPage'); } } else { ToastHelper.showErrorToast(context, 'Invalid OTP. Please try again');