This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
LocalSpend-Tracker/lib/common/apifunctions/request_login_api.dart

70 lines
1.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:local_spend/common/functions/save_current_login.dart';
import 'package:local_spend/model/json/login_model.dart';
Future<void> _incorrectDialog(BuildContext context, bool isLoginWrong) async {
2019-08-12 14:18:01 +00:00
return showDialog<void>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Unable to Login"),
2019-08-16 13:22:06 +00:00
content: Text(isLoginWrong ? "Incorrect login details. Please try again." : "Our servers are having issues at the moment; sorry for the inconvenience. Please try again later."),
2019-08-12 14:18:01 +00:00
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
Future<LoginModel> requestLoginAPI(
BuildContext context, String email, String password) async {
//var apiUrl = ConfigWrapper.of(context).apiKey;
final url = "https://dev.peartrade.org/api/login";
Map<String, String> body = {
'email': email,
'password': password,
};
2019-07-16 11:09:10 +00:00
// debugPrint('$body');
try {
final response = await http.post(
url,
body: json.encode(body),
);
if (response.statusCode == 200) {
final responseJson = json.decode(response.body);
saveCurrentLogin(responseJson, body["email"]);
Navigator.of(context).pushReplacementNamed('/HomePage');
return LoginModel.fromJson(responseJson);
} else {
final responseJson = json.decode(response.body);
saveCurrentLogin(responseJson, body["email"]);
_incorrectDialog(context, true);
2019-07-16 11:09:10 +00:00
return null;
}
2019-08-16 13:23:03 +00:00
} catch (error) {
debugPrint(error.toString());
_incorrectDialog(context, false);
}
}