From 7651d5200767a339089dd4021bf77a86b6adcf80 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 11 Sep 2023 01:00:14 +0530 Subject: first commit --- lib/models/login.dart | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lib/models/login.dart (limited to 'lib/models/login.dart') diff --git a/lib/models/login.dart b/lib/models/login.dart new file mode 100644 index 0000000..6412921 --- /dev/null +++ b/lib/models/login.dart @@ -0,0 +1,66 @@ +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); +} -- cgit v1.2.3