createAccount static method

Future<void> createAccount(
  1. Map<String, dynamic> userData, {
  2. bool autoLogin = true,
})

Implementation

static Future<void> createAccount(Map<String, dynamic> userData, {bool autoLogin = true}) async {
  await initialize();
  logger.finest('createAccount start');
  // isRemoved
  userData['isRemoved'] = userData['isRemoved'] ?? false;
  // accountSignUpType
  var accountSignUpType = AccountSignUpType.hycop;
  if (userData['accountSignUpType'] == null) {
    userData['accountSignUpType'] = accountSignUpType.index;
  } else {
    accountSignUpType =
        AccountSignUpType.fromInt(int.parse(userData['accountSignUpType'].toString()));
    if (accountSignUpType == AccountSignUpType.none) {
      logger.severe('invalid sign-up type !!!');
      throw HycopUtils.getHycopException(defaultMessage: 'invalid sign-up type !!!');
    }
  }
  logger.info('accountSignUpType($accountSignUpType)');
  // password
  String password = userData['password'] ?? '';
  if (password.isEmpty && accountSignUpType == AccountSignUpType.hycop) {
    // hycop-service need password !!!
    logger.severe('password is empty !!!');
    throw HycopUtils.getHycopException(defaultMessage: 'password is empty !!!');
  }
  String email = userData['email'] ?? '';
  String passwordSha1 = '';
  if (accountSignUpType == AccountSignUpType.hycop) {
    // hycop-service's password = sha1-hash of password
    passwordSha1 = HycopUtils.stringToSha1(password);
  } else {
    // not hycop-service's password = sha1-hash of email
    if (email.isEmpty) {
      logger.severe('email is empty !!!');
      throw HycopUtils.getHycopException(defaultMessage: 'email is empty !!!');
    }
    passwordSha1 = HycopUtils.stringToSha1(email);
    password = passwordSha1;
  }
  userData['password'] = passwordSha1;
  logger.finest('password resetting to [$password] (${accountSignUpType.name}');
  // check exist
  final value = await AccountManager.isExistAccount(email).catchError((error, stackTrace) {
    throw HycopUtils.getHycopException(error: error, defaultMessage: 'isExistAccount error !!!');
  });
  switch (value) {
    case AccountSignUpType.hycop:
      // already exist ==> not create ==> login
      throw HycopUtils.getHycopException(defaultMessage: 'already exist account !!!');
    case AccountSignUpType.google:
      // google account ==> cannot create ==> throw exception
      throw HycopUtils.getHycopException(defaultMessage: 'cannot create google account !!!');
    case AccountSignUpType.removed:
      // removed account ==> cannot create ==> throw exception
      throw HycopUtils.getHycopException(
        defaultMessage: 'before removed account !!!',
        error: HycopException.emailBeforeRemoved,
      );
    case AccountSignUpType.none:
    default:
      // not exist ==> createAccount
      await HycopFactory.account!.createAccount(userData).catchError((error, stackTrace) =>
          throw HycopUtils.getHycopException(
              error: error, defaultMessage: 'AccountManager.createAccount Failed !!!'));
  }
  logger.finest('createAccount end');

  if (autoLogin) {
    _currentLoginUser = UserModel(userData: userData);
    await generateToken();
  }
  logger.finest('createAccount set');
}