createUser static method

Future createUser(
  1. String uid, {
  2. String? firstName,
  3. String? lastName,
  4. String? email,
  5. String? phoneNo,
  6. String? countryCode,
  7. String? residentCountry,
  8. String? referralCode,
  9. String? type,
})

Implementation

static Future createUser(String uid,
    {String? firstName,
      String? lastName,
      String? email,
      String? phoneNo,
      String? countryCode,
      String? residentCountry,
      String? referralCode,
      String? type}) async {
  try {
    var headers = {
      "Access-Control-Allow-Origin": "*",
      'Content-Type': 'application/json',
      'Accept': '*/*'
    };
    var request = http.Request('POST', Uri.parse('${serverUrl}auth/signin'));
    String? token = await FirebaseAuth.instance.currentUser?.getIdToken();
     DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      WebBrowserInfo webBrowserInfo= await  deviceInfo.webBrowserInfo;

    Map<String, dynamic> payload = {};
    if (token != null) {
      payload = {
        "token": token,
        "referralCode": referralCode,
        "phone": phoneNo?.isNotEmpty == true ? phoneNo : null,
        "type": type,
        "firstName": firstName,
        "countryCode": countryCode?.isNotEmpty == true ? countryCode : null,
        "lastName": lastName,
        "device": webBrowserInfo.browserName.name
      };
    } else {
      payload = {
        "uid": uid,
        "firstName": firstName,
        "lastName": lastName,
        "email": email,
        "phone": phoneNo?.isNotEmpty == true ? phoneNo : null,
        "countryCode": countryCode?.isNotEmpty == true ? countryCode : null,
        "residentCountry": residentCountry,
        "referralCode": referralCode,
        "type": type,
        "device": webBrowserInfo.browserName.name
      };
    }
    payload.removeWhere((key, value) => value == null);
    request.body = json.encode(payload);
    print("payload Sent${request.body.toString()}");
    request.headers.addAll(headers);
    http.StreamedResponse response = await request.send();
    String? role;
    String? referrerCode;
    String? message;
    String? phone;
    if (response.statusCode == 200) {
      String responseText = await response.stream.bytesToString();
      print(responseText.toString());
      var jsonResponse = jsonDecode(responseText);
      role = jsonResponse['data']['userInfo']['role'];
      cookie = jsonResponse["data"]["token"]["token"];
      token = jsonResponse["data"]["token"]["token"];
      referrerCode = jsonResponse['data']['userInfo']['referrerCode'];
      phone =  jsonResponse['data']['phone'];
      print("cookie${cookie}");

      final box = GetStorage();
      box.write('cookie', cookie);
      box.write('token', token);
      await box.save();
    } else {
      print("signup error${response.statusCode}");
      String? responseText = await response.stream.bytesToString();
      print(responseText);
      var jsonResponse = jsonDecode(responseText);
      message = jsonResponse['message'];
    }
    print("status code ${response.statusCode}");
    return {
      'statusCode': response.statusCode,
      'role': role,
      'referrerCode': referrerCode,
      'message':message,
      'phone':phone
    };
  } catch (e) {
    if (kDebugMode) {
      print("signup error${e.toString()}");
    }
  }
}