aboutsummaryrefslogtreecommitdiff
path: root/lib/models/user.dart
blob: 07c2c092db2ef8d87a9776b93d6494e5abc7e45f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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<String, dynamic> json) {
    return User(
      id: json["ID"],
      createdAt: json["CreatedAt"],
      updatedAt: json["UpdatedAt"],
      username: json["Username"],
      email: json["Email"],
      isVerified: json["IsVerified"]
    );
  }
}

Future<User> userSignUp(User newUser, String password) async {
  final res = await http.post(
    Uri.parse("https://openbills.vidhukant.com/api/auth/signup"),
    headers: <String, String> {
      "Content-Type": "application/json; charset=UTF-8",
    },
    body: jsonEncode(<String, String> {
      "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"]);
}