From c288f257c56daa38310fce5714f8731c1ae074e1 Mon Sep 17 00:00:00 2001 From: Felix Date: Tue, 16 Jul 2019 12:09:10 +0100 Subject: [PATCH] save login works --- .../apifunctions/request_login_api.dart | 9 +++--- .../apifunctions/request_logout_api.dart | 4 +-- .../apifunctions/submit_receipt_api.dart | 8 ++--- lib/common/functions/logout.dart | 10 ++++++ lib/common/widgets/basic_drawer.dart | 2 +- lib/pages/login_page.dart | 32 ++++++++++++++++--- lib/pages/receipt_page.dart | 2 +- 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/lib/common/apifunctions/request_login_api.dart b/lib/common/apifunctions/request_login_api.dart index 2e31f8e..c1ac5e3 100644 --- a/lib/common/apifunctions/request_login_api.dart +++ b/lib/common/apifunctions/request_login_api.dart @@ -20,14 +20,14 @@ Future requestLoginAPI( 'password': password, }; - debugPrint('$body'); +// debugPrint('$body'); final response = await http.post( url, body: json.encode(body), ); - debugPrint(response.body); +// debugPrint(response.body); if (response.statusCode == 200) { final responseJson = json.decode(response.body); @@ -38,17 +38,18 @@ Future requestLoginAPI( return LoginModel.fromJson(responseJson); } else { - debugPrint("Invalid, either creds are wrong or server is down"); - Navigator.of(context).pushReplacementNamed('/HomePage'); // just here temporarily while server is down +// debugPrint("Invalid, either credentials are wrong or server is down"); final responseJson = json.decode(response.body); saveCurrentLogin(responseJson, body["email"]); + showDialogSingleButton( context, "Unable to Login", "You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.", "OK"); + return null; } } diff --git a/lib/common/apifunctions/request_logout_api.dart b/lib/common/apifunctions/request_logout_api.dart index 17a63a2..342c85b 100644 --- a/lib/common/apifunctions/request_logout_api.dart +++ b/lib/common/apifunctions/request_logout_api.dart @@ -26,12 +26,12 @@ Future requestLogoutAPI(BuildContext context) async { ); if (response.statusCode == 200) { - debugPrint("Logout successful: " + response.body); +// debugPrint("Logout successful: " + response.body); saveLogout(); return null; } else { - debugPrint("Logout unsuccessful: " + response.body); +// debugPrint("Logout unsuccessful: " + response.body); saveLogout(); return null; diff --git a/lib/common/apifunctions/submit_receipt_api.dart b/lib/common/apifunctions/submit_receipt_api.dart index 7c68ca3..bfbabfc 100644 --- a/lib/common/apifunctions/submit_receipt_api.dart +++ b/lib/common/apifunctions/submit_receipt_api.dart @@ -46,20 +46,20 @@ Future submitReceiptAPI( 'session_key': preferences.get('LastToken'), }; - debugPrint('$body'); - debugPrint(json.encode(body)); +// debugPrint('$body'); +// debugPrint(json.encode(body)); final response = await http.post( url, body: json.encode(body), ); - debugPrint(response.body); +// debugPrint(response.body); if (response.statusCode == 200) { final responseJson = json.decode(response.body); - print(responseJson[0]); +// print(responseJson[0]); showDialogSingleButton( context, diff --git a/lib/common/functions/logout.dart b/lib/common/functions/logout.dart index 8f9ba29..e05447d 100644 --- a/lib/common/functions/logout.dart +++ b/lib/common/functions/logout.dart @@ -1,7 +1,17 @@ import 'package:flutter/material.dart'; import 'package:local_spend/common/apifunctions/request_logout_api.dart'; +import 'package:shared_preferences/shared_preferences.dart'; logout(context) { requestLogoutAPI(context); Navigator.of(context).pushReplacementNamed('/LoginPage'); + _clearLoginDetails(); +} + +_clearLoginDetails() async { + SharedPreferences preferences = await SharedPreferences.getInstance(); + + preferences.setString('username', ""); + preferences.setString('password', ""); + print("details cleared"); } \ No newline at end of file diff --git a/lib/common/widgets/basic_drawer.dart b/lib/common/widgets/basic_drawer.dart index f19c835..b03085a 100644 --- a/lib/common/widgets/basic_drawer.dart +++ b/lib/common/widgets/basic_drawer.dart @@ -37,7 +37,7 @@ class _BasicDrawerState extends State { style: TextStyle(color: Colors.black, fontSize: 20.0), ), onTap: () { - debugPrint('$token'); +// debugPrint('$token'); Navigator.of(context).pushNamed('/ReceiptPage'); }, // enabled: token != null && token.isNotEmpty, diff --git a/lib/pages/login_page.dart b/lib/pages/login_page.dart index 2d377a9..b653bc5 100644 --- a/lib/pages/login_page.dart +++ b/lib/pages/login_page.dart @@ -19,8 +19,8 @@ class LoginPage extends StatefulWidget { } class LoginPageState extends State { - final TextEditingController _emailController = TextEditingController(text: 'test@example.com'); // remove - final TextEditingController _passwordController = TextEditingController(text: 'abc123'); // remove + final TextEditingController _emailController = TextEditingController(/*text: 'test@example.com'*/); // remove + final TextEditingController _passwordController = TextEditingController(/*text: 'abc123'*/); // remove bool _saveLoginDetails = true; // I am extremely sorry for the placement of this variable // it will be fixed soon I promise @@ -44,6 +44,8 @@ class LoginPageState extends State { _saveCurrentRoute("/LoginPage"); focusNode = FocusNode(); + + _fillLoginDetails(); } @override @@ -52,13 +54,35 @@ class LoginPageState extends State { super.dispose(); } + _fillLoginDetails() async { + SharedPreferences preferences = await SharedPreferences.getInstance(); + + var username = await preferences.get('username'); + var password = await preferences.get('password'); + + _emailController.text = await username; + _passwordController.text = await password; + } + _saveCurrentRoute(String lastRoute) async { SharedPreferences preferences = await SharedPreferences.getInstance(); await preferences.setString('LastPageRoute', lastRoute); } - void login(String username, String password) { + login(String username, String password) async { SystemChannels.textInput.invokeMethod('TextInput.hide'); + SharedPreferences preferences = await SharedPreferences.getInstance(); + + if (_saveLoginDetails) { + await preferences.setString('username', username); + await preferences.setString('password', password); + print("details saved"); + } else { + await preferences.setString('username', ""); + await preferences.setString('password', ""); + print("details cleared"); + } + requestLoginAPI(context, username, password); } @@ -174,7 +198,7 @@ class LoginPageState extends State { gradient: new LinearGradient( colors: [ Colors.blue[300], - Colors.blue[600], + Colors.blue[500], ], stops: [0,1], begin: Alignment.topLeft, diff --git a/lib/pages/receipt_page.dart b/lib/pages/receipt_page.dart index ccfd83e..fb56640 100644 --- a/lib/pages/receipt_page.dart +++ b/lib/pages/receipt_page.dart @@ -106,7 +106,7 @@ class ReceiptPageState extends State { // setting up 'receipt' receipt.amount = amount; receipt.time = formatDate(time); - debugPrint(organisation.name + ", " + organisation.streetName + ", " + organisation.town + ", " + organisation.postcode); +// debugPrint(organisation.name + ", " + organisation.streetName + ", " + organisation.town + ", " + organisation.postcode); receipt.organisationName = organisation.name; receipt.street = organisation.streetName; receipt.town = organisation.town;