signIn method

Future<void> signIn({
  1. required SignInAuthProvider provider,
})

Sign-in is performed by passing a class inheriting from SignInAuthProvider as provider.

SignInAuthProviderを継承したクラスをproviderで渡すことにより、サインインを行ないます。

Implementation

Future<void> signIn({
  required SignInAuthProvider provider,
}) async {
  await _initialize();
  final accounts = _data._getAccounts();
  if (provider is DirectSignInAuthProvider) {
    final active = this.active;
    if (active.isEmpty) {
      final current = _data._getCurrent();
      final userId = current.get(userIdKey, "");
      if (userId.isEmpty) {
        final userId = provider.userId;
        _data._setAccount(userId, {
          userIdKey: userId,
          activeProvidersKey: [provider.providerId].distinct(),
        });
        _data._setCurrent(userId);
        _activeId = userId;
      } else {
        final current = _data._getAccount(userId);
        final providers = [
          ...current.getAsList(activeProvidersKey),
          provider.providerId
        ].distinct();
        if (!provider.allowMultiProvider && providers.length > 1) {
          throw Exception(
            "Signing in with multiple providers is not allowed.",
          );
        }
        _data._setAccount(userId, {
          ...current,
          activeProvidersKey: providers,
        });
        _activeId = userId;
      }
    } else {
      final userId = _activeId!;
      final current = _data._getAccount(userId);
      final providers = [
        ...current.getAsList(activeProvidersKey),
        provider.providerId
      ].distinct();
      if (!provider.allowMultiProvider && providers.length > 1) {
        throw Exception(
          "Signing in with multiple providers is not allowed.",
        );
      }
      _data._setAccount(userId, {
        ...current,
        activeProvidersKey: providers,
      });
    }
  } else if (provider is AnonymouslySignInAuthProvider ||
      provider is SnsSignInAuthProvider) {
    final active = this.active;
    if (active.isEmpty) {
      final current = _data._getCurrent();
      final userId = current.get(userIdKey, "");
      if (userId.isEmpty) {
        final userId = _uuid;
        _data._setAccount(userId, {
          userIdKey: userId,
          anonymouslyKey: true,
          activeProvidersKey: [provider.providerId].distinct(),
        });
        _data._setCurrent(userId);
        _activeId = userId;
      } else {
        final current = _data._getAccount(userId);
        final providers = [
          ...current.getAsList(activeProvidersKey),
          provider.providerId
        ].distinct();
        if (!provider.allowMultiProvider && providers.length > 1) {
          throw Exception(
            "Signing in with multiple providers is not allowed.",
          );
        }
        _data._setAccount(userId, {
          ...current,
          anonymouslyKey: true,
          activeProvidersKey: providers,
        });
        _activeId = userId;
      }
    } else {
      final userId = _activeId!;
      final current = _data._getAccount(userId);
      final providers = [
        ...current.getAsList(activeProvidersKey),
        provider.providerId
      ].distinct();
      if (!provider.allowMultiProvider && providers.length > 1) {
        throw Exception(
          "Signing in with multiple providers is not allowed.",
        );
      }
      _data._setAccount(userId, {
        ...current,
        anonymouslyKey: true,
        activeProvidersKey: providers,
      });
    }
  } else if (provider is EmailAndPasswordSignInAuthProvider) {
    final accounts = _data._getAccounts();
    final account = accounts.firstWhereOrNull((e) =>
        e.get(userEmailKey, "") == provider.email &&
        e.get(userPasswordKey, "") == provider.password);
    if (account == null) {
      throw Exception("Email or Password is invalid.");
    }
    final userId = account.get(userIdKey, "");
    _data._setCurrent(userId);
    _activeId = userId;
  } else if (provider is EmailLinkSignInAuthProvider) {
    await onSendEmailLink?.call(
      provider.email,
      provider.url,
      provider.locale ?? defaultLocale,
    );
    final account = accounts
        .firstWhereOrNull((e) => e.get(userEmailKey, "") == provider.email);
    if (account == null) {
      final userId = _uuid;
      _data._setAccount(userId, {
        userIdKey: userId,
        emailLinkUrlKey: provider.url,
        tmpUserEmailKey: provider.email,
      });
      _data._setTemporary(
        userId,
        allowMultiProvider: provider.allowMultiProvider,
      );
    } else {
      final userId = account.get(userIdKey, "");
      final providers = [
        ...account.getAsList(activeProvidersKey),
        provider.providerId
      ].distinct();
      if (!provider.allowMultiProvider && providers.length > 1) {
        throw Exception(
          "Signing in with multiple providers is not allowed.",
        );
      }
      _data._setAccount(userId, {
        ...account,
        emailLinkUrlKey: provider.url,
        tmpUserEmailKey: provider.email,
      });
      _data._setTemporary(
        userId,
        allowMultiProvider: provider.allowMultiProvider,
      );
    }
  } else if (provider is SmsSignInAuthProvider) {
    final account = accounts.firstWhereOrNull(
        (e) => e.get(userPhoneNumberKey, "") == provider.phoneNumber);
    final code = debugSmsCode ?? generateCode(6, charSet: "0123456789");
    await onSendSMS?.call(
      provider.phoneNumber,
      code,
      provider.locale ?? defaultLocale,
    );
    if (account == null) {
      final userId = _uuid;
      _data._setAccount(userId, {
        userIdKey: userId,
        tmpUserPhoneNumberKey: provider.phoneNumber,
        smsCodeKey: code,
      });
      _data._setTemporary(
        userId,
        allowMultiProvider: provider.allowMultiProvider,
      );
    } else {
      final userId = account.get(userIdKey, "");
      final providers = [
        ...account.getAsList(activeProvidersKey),
        provider.providerId
      ].distinct();
      if (!provider.allowMultiProvider && providers.length > 1) {
        throw Exception(
          "Signing in with multiple providers is not allowed.",
        );
      }
      _data._setAccount(userId, {
        ...account,
        tmpUserPhoneNumberKey: provider.phoneNumber,
        smsCodeKey: code,
      });
      _data._setTemporary(
        userId,
        allowMultiProvider: provider.allowMultiProvider,
      );
    }
  }
  await onSaved?.call(this);
}