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/instance.dart | 27 ++++++++++++++++++++ lib/models/login.dart | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/models/user.dart | 61 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 lib/models/instance.dart create mode 100644 lib/models/login.dart create mode 100644 lib/models/user.dart (limited to 'lib/models') diff --git a/lib/models/instance.dart b/lib/models/instance.dart new file mode 100644 index 0000000..b01b052 --- /dev/null +++ b/lib/models/instance.dart @@ -0,0 +1,27 @@ +import "package:http/http.dart" as http; +import "dart:convert"; + +class Instance { + // TODO: add other fields + final String version; + + Instance({ + required this.version, + }); + + factory Instance.fromJson(Map json) { + return Instance( + version: json["Version"], + ); + } +} + +Future getInstance(String instanceURL) async { + final res = await http.get(Uri.parse("$instanceURL/info")); + + final json = jsonDecode(res.body); + + // TODO: handle errors + + return Instance.fromJson(json); +} \ No newline at end of file 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); +} diff --git a/lib/models/user.dart b/lib/models/user.dart new file mode 100644 index 0000000..07c2c09 --- /dev/null +++ b/lib/models/user.dart @@ -0,0 +1,61 @@ +import 'package:http/http.dart' as http; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import "dart:convert"; + +class User { + final int id; + String? createdAt; + String? updatedAt; + String? username; + String? email; + bool? isVerified; + + User({ + required this.id, + this.createdAt, + this.updatedAt, + this.username, + this.email, + this.isVerified + }); + + factory User.fromJson(Map json) { + return User( + id: json["ID"], + createdAt: json["CreatedAt"], + updatedAt: json["UpdatedAt"], + username: json["Username"], + email: json["Email"], + isVerified: json["IsVerified"] + ); + } +} + +Future userSignUp(User newUser, String password) async { + final res = await http.post( + Uri.parse("https://openbills.vidhukant.com/api/auth/signup"), + headers: { + "Content-Type": "application/json; charset=UTF-8", + }, + body: jsonEncode( { + "Username": newUser.username as String, + "Password": password, + "Email": newUser.email as String, + }), + ); + + 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 User.fromJson(json["data"]); +} -- cgit v1.2.3