loginEmail method
Implementation
Future<Map?> loginEmail(String email, String password) async {
dynamic returnable;
await SharedPreferences.getInstance().then((pref) async {
Map data = {'username': email, 'password': password};
String? client_id = iAmA == "consultant"
? Params.consultant_client_id
: Params.patient_client_id;
/*Calling the API url */
var jsonData;
final response = await http.post(
Uri.parse(Params.base_url + "/accounts/auth/jwt/create/"),
body: data,
headers: {"Client-ID": "$client_id"});
if (debug) {
print('Status Code = ' +
response.statusCode.toString() +
". Response: " +
response.body);
}
/*If the response is 200 or 201 (success) then the response will be decoded */
if (response.statusCode == 200 || response.statusCode == 201) {
print("Response is success! ");
jsonData = json.decode(response.body);
pref.setBool('loggedIn', true);
pref.setString('refresh_token', jsonData["refresh"]);
pref.setString('token', jsonData["access"]);
print("Logged in");
returnable = {
"status": "success",
"refresh_token": jsonData["refresh"],
"token": jsonData["access"],
};
// });
} else if (response.statusCode == 401) {
jsonData = json.decode(response.body);
returnable = {
"status": "failed",
"error": jsonData["detail"],
};
} else {
if (debug) print("Error Requesting API");
returnable = null;
//return null;
}
});
return returnable;
}