login method

Future<LoginSession> login({
  1. String? userId,
  2. String? username,
  3. String? email,
  4. required String password,
})

Implementation

Future<dart_blocks.LoginSession> login({
  String? userId,
  String? username,
  String? email,
  required String password,
}) async {
  if ((userId == null || userId == "") &&
      (username == null || username == "") &&
      (email == null || email == "")) {
    throw Exception(
        "missing one required identifier: userId, username or email");
  }
  String cloudToken = await _authorize.getAccessToken();
  dart_blocks.UserRequest req = dart_blocks.UserRequest();
  dart_blocks.User user = dart_blocks.User();
  user.id = userId ?? "";
  user.username = username ?? "";
  user.email = email ?? "";
  user.password = password;
  req.cloudToken = cloudToken;
  req.encryptionKey = _encryptionKey ?? "";
  req.user = user;
  // set metadata for token
  dart_blocks.Token _token = dart_blocks.Token();
  _token.deviceInfo = await _getDeviceInfo();
  var _placemark = await _determinePlacemark();
  if (_placemark != null) {
    dart_blocks.Location _location = dart_blocks.Location();
    _location.country = _placemark.country ?? "";
    _location.countryCode = _placemark.isoCountryCode ?? "";
    _location.city = _placemark.locality ?? "";
    _token.loggedInFrom = _location;
  }
  req.token = _token;
  try {
    dart_blocks.UserResponse resp = await _grpcUserClient.login(req);
    if (resp.loginSession.loginStatus !=
        dart_blocks.LoginStatus.AUTHENTICATED) {
      return resp.loginSession;
    }
    if (resp.token.accessToken == "" || resp.token.refreshToken == "") {
      throw Exception("token is null. Contact info@nuntio.io");
    }
    if (resp.user.id == "") {
      throw Exception("user is null. Contact info@nuntio.io");
    }
    _setCurrentUser(resp.user);
    _setAccessToken(resp.token.accessToken);
    _setRefreshToken(resp.token.refreshToken);
  } catch (e) {
    if (debug == true) print("could not login user: " + e.toString());
    rethrow;
  }
  return dart_blocks.LoginSession()
    ..loginStatus = dart_blocks.LoginStatus.AUTHENTICATED;
}