import "package:http/http.dart" as http; import "dart:convert"; import "package:openbills/models/user.dart"; enum LoginMethod { email, username } extension LoginMethodString on LoginMethod { String get value { switch(this) { case LoginMethod.username: return "username"; case LoginMethod.email: return "email"; } } } class LoginRes { final User user; final String authToken; final String refreshToken; LoginRes({ required this.user, required this.authToken, required this.refreshToken, }); factory LoginRes.fromJson(Map json) { return LoginRes( user: User.fromJson(json["data"]), authToken: json["auth_token"], refreshToken: json["refresh_token"], ); } } Future userSignIn(String accountName, method, password) async { final res = await http.post( Uri.parse("https://openbills.vidhukant.com/api/auth/signin"), headers: { "Content-Type": "application/json; charset=UTF-8", }, body: jsonEncode( { "AccountName": accountName, "Method": method, "Password": password, }) ); final json = jsonDecode(res.body); if (res.statusCode != 200) { switch(res.statusCode) { case 404: throw "This user does not exist"; case 500: throw "Internal Server Error"; default: throw json["error"]; } } return LoginRes.fromJson(json); }