loginWithKakao method

  1. @override
dynamic loginWithKakao()
override

Implementation

@override
loginWithKakao() async {
  update(state: LoginViewState.Loading);

  _KakaoLoginInfo? loginInfo = await loginKakao();

  if (loginInfo == null) {
    this.authError = AuthError(
        title: AppStrings.loginError, code: AppStrings.codeKakaoLoginError);
    update(state: LoginViewState.Error);
  } else {
    var credential = await repository.loginWithKakao(KakaoCredential(
        accessToken: loginInfo.accessToken, id: loginInfo.userId));

    if (credential is ICredential) {
      if (credential.isError) {
        this.authError = credential.error;
        await UserApi.instance.logout();
        update(state: LoginViewState.Error);
        return;
      }

      await Future.wait([
        AuthRepository.instance().writeUserId(credential.userId),
        AuthRepository.instance().writeUserCode(credential.userCode),
        AuthRepository.instance().writeAccessToken(credential.accessToken),
        AuthRepository.instance()
            .writeRefreshToken(credential.refreshToken ?? "")
      ]);

      await UserApi.instance.logout();
      update(state: LoginViewState.Success);
    } else {
      this.authError = AuthError(
          title: AppStrings.loginError, code: AppStrings.codeKakaoLoginError);
      await UserApi.instance.logout();
      update(state: LoginViewState.Error);
    }
  }
}