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/user.dart | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/models/user.dart (limited to 'lib/models/user.dart') 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