😅

[Firebase Auth] user-not-foundを拾いたいのに、invalid-credentialが出力される問題

2024/05/07に公開1

問題

FlutterでFirebase Authを用いた認証を実装していて、未登録のユーザーのサインインの例外処理を実装していた際に、user-not-foundではなく、invalid-credentialが出力される

コード

  Future<void> _signIn() async {
    try {
      await FirebaseAuth.instance.signInWithEmailAndPassword(
        email: _emailController.text,
        password: _passwordController.text,
      );
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (context) => HomePage()),
      );
    } on FirebaseAuthException catch (e) {
      debugPrint(e.toString());
      if (e.code == '') {
        _showSnackBar('ユーザーが登録されていません');
      } else if (e.code == 'wrong-password') {
        _showSnackBar('パスワードが間違っています');
      } 
    }
  }

やったこと

Firebaseコンソールでメールの列挙保護をオフにした。ちなみに、非推奨のため、今後修正する必要がありそう。

参考リンク

おわりに

人生初の技術記事です

Discussion

osakiosaki

https://cloud.google.com/identity-platform/docs/admin/email-enumeration-protection?hl=ja

まさに上記に記載されているとおりで、_showSnackBar('ユーザーが登録されていません')_showSnackBar('パスワードが間違っています')といったことを避けるべきなのでしょうね。invalid-credentialのままとすべきかなと思いました。

ご参考まで: