save login works
This commit is contained in:
parent
4c036b8bf2
commit
c288f257c5
7 changed files with 51 additions and 16 deletions
|
@ -20,14 +20,14 @@ Future<LoginModel> requestLoginAPI(
|
||||||
'password': password,
|
'password': password,
|
||||||
};
|
};
|
||||||
|
|
||||||
debugPrint('$body');
|
// debugPrint('$body');
|
||||||
|
|
||||||
final response = await http.post(
|
final response = await http.post(
|
||||||
url,
|
url,
|
||||||
body: json.encode(body),
|
body: json.encode(body),
|
||||||
);
|
);
|
||||||
|
|
||||||
debugPrint(response.body);
|
// debugPrint(response.body);
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final responseJson = json.decode(response.body);
|
final responseJson = json.decode(response.body);
|
||||||
|
@ -38,17 +38,18 @@ Future<LoginModel> requestLoginAPI(
|
||||||
|
|
||||||
return LoginModel.fromJson(responseJson);
|
return LoginModel.fromJson(responseJson);
|
||||||
} else {
|
} else {
|
||||||
debugPrint("Invalid, either creds are wrong or server is down");
|
// debugPrint("Invalid, either credentials are wrong or server is down");
|
||||||
Navigator.of(context).pushReplacementNamed('/HomePage'); // just here temporarily while server is down
|
|
||||||
|
|
||||||
final responseJson = json.decode(response.body);
|
final responseJson = json.decode(response.body);
|
||||||
|
|
||||||
saveCurrentLogin(responseJson, body["email"]);
|
saveCurrentLogin(responseJson, body["email"]);
|
||||||
|
|
||||||
showDialogSingleButton(
|
showDialogSingleButton(
|
||||||
context,
|
context,
|
||||||
"Unable to Login",
|
"Unable to Login",
|
||||||
"You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
|
"You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
|
||||||
"OK");
|
"OK");
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,12 +26,12 @@ Future<LoginModel> requestLogoutAPI(BuildContext context) async {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
debugPrint("Logout successful: " + response.body);
|
// debugPrint("Logout successful: " + response.body);
|
||||||
|
|
||||||
saveLogout();
|
saveLogout();
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
debugPrint("Logout unsuccessful: " + response.body);
|
// debugPrint("Logout unsuccessful: " + response.body);
|
||||||
|
|
||||||
saveLogout();
|
saveLogout();
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -46,20 +46,20 @@ Future<LoginModel> submitReceiptAPI(
|
||||||
'session_key': preferences.get('LastToken'),
|
'session_key': preferences.get('LastToken'),
|
||||||
};
|
};
|
||||||
|
|
||||||
debugPrint('$body');
|
// debugPrint('$body');
|
||||||
debugPrint(json.encode(body));
|
// debugPrint(json.encode(body));
|
||||||
|
|
||||||
final response = await http.post(
|
final response = await http.post(
|
||||||
url,
|
url,
|
||||||
body: json.encode(body),
|
body: json.encode(body),
|
||||||
);
|
);
|
||||||
|
|
||||||
debugPrint(response.body);
|
// debugPrint(response.body);
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final responseJson = json.decode(response.body);
|
final responseJson = json.decode(response.body);
|
||||||
|
|
||||||
print(responseJson[0]);
|
// print(responseJson[0]);
|
||||||
|
|
||||||
showDialogSingleButton(
|
showDialogSingleButton(
|
||||||
context,
|
context,
|
||||||
|
|
|
@ -1,7 +1,17 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:local_spend/common/apifunctions/request_logout_api.dart';
|
import 'package:local_spend/common/apifunctions/request_logout_api.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
logout(context) {
|
logout(context) {
|
||||||
requestLogoutAPI(context);
|
requestLogoutAPI(context);
|
||||||
Navigator.of(context).pushReplacementNamed('/LoginPage');
|
Navigator.of(context).pushReplacementNamed('/LoginPage');
|
||||||
|
_clearLoginDetails();
|
||||||
|
}
|
||||||
|
|
||||||
|
_clearLoginDetails() async {
|
||||||
|
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
|
preferences.setString('username', "");
|
||||||
|
preferences.setString('password', "");
|
||||||
|
print("details cleared");
|
||||||
}
|
}
|
|
@ -37,7 +37,7 @@ class _BasicDrawerState extends State<BasicDrawer> {
|
||||||
style: TextStyle(color: Colors.black, fontSize: 20.0),
|
style: TextStyle(color: Colors.black, fontSize: 20.0),
|
||||||
),
|
),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
debugPrint('$token');
|
// debugPrint('$token');
|
||||||
Navigator.of(context).pushNamed('/ReceiptPage');
|
Navigator.of(context).pushNamed('/ReceiptPage');
|
||||||
},
|
},
|
||||||
// enabled: token != null && token.isNotEmpty,
|
// enabled: token != null && token.isNotEmpty,
|
||||||
|
|
|
@ -19,8 +19,8 @@ class LoginPage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class LoginPageState extends State<LoginPage> {
|
class LoginPageState extends State<LoginPage> {
|
||||||
final TextEditingController _emailController = TextEditingController(text: 'test@example.com'); // remove
|
final TextEditingController _emailController = TextEditingController(/*text: 'test@example.com'*/); // remove
|
||||||
final TextEditingController _passwordController = TextEditingController(text: 'abc123'); // remove
|
final TextEditingController _passwordController = TextEditingController(/*text: 'abc123'*/); // remove
|
||||||
bool _saveLoginDetails = true; // I am extremely sorry for the placement of this variable
|
bool _saveLoginDetails = true; // I am extremely sorry for the placement of this variable
|
||||||
// it will be fixed soon I promise
|
// it will be fixed soon I promise
|
||||||
|
|
||||||
|
@ -44,6 +44,8 @@ class LoginPageState extends State<LoginPage> {
|
||||||
_saveCurrentRoute("/LoginPage");
|
_saveCurrentRoute("/LoginPage");
|
||||||
|
|
||||||
focusNode = FocusNode();
|
focusNode = FocusNode();
|
||||||
|
|
||||||
|
_fillLoginDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -52,13 +54,35 @@ class LoginPageState extends State<LoginPage> {
|
||||||
super.dispose();
|
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 {
|
_saveCurrentRoute(String lastRoute) async {
|
||||||
SharedPreferences preferences = await SharedPreferences.getInstance();
|
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||||
await preferences.setString('LastPageRoute', lastRoute);
|
await preferences.setString('LastPageRoute', lastRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
void login(String username, String password) {
|
login(String username, String password) async {
|
||||||
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
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,
|
requestLoginAPI(context, username,
|
||||||
password);
|
password);
|
||||||
}
|
}
|
||||||
|
@ -174,7 +198,7 @@ class LoginPageState extends State<LoginPage> {
|
||||||
gradient: new LinearGradient(
|
gradient: new LinearGradient(
|
||||||
colors: [
|
colors: [
|
||||||
Colors.blue[300],
|
Colors.blue[300],
|
||||||
Colors.blue[600],
|
Colors.blue[500],
|
||||||
],
|
],
|
||||||
stops: [0,1],
|
stops: [0,1],
|
||||||
begin: Alignment.topLeft,
|
begin: Alignment.topLeft,
|
||||||
|
|
|
@ -106,7 +106,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
||||||
// setting up 'receipt'
|
// setting up 'receipt'
|
||||||
receipt.amount = amount;
|
receipt.amount = amount;
|
||||||
receipt.time = formatDate(time);
|
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.organisationName = organisation.name;
|
||||||
receipt.street = organisation.streetName;
|
receipt.street = organisation.streetName;
|
||||||
receipt.town = organisation.town;
|
receipt.town = organisation.town;
|
||||||
|
|
Reference in a new issue