LoginSignup
1
1

Amazon Cognito+AWS Amplifyを使ったログイン機能でサインインだけエラーハンドリングできない問題の解決方法

Last updated at Posted at 2023-09-29

Amazon Cognito+AWS Amplifyを使用してログイン機能を導入していた際、
サインインの画面でだけエラーハンドリングができない問題が発生しました。

解決できたので共有いたします。

コード

    func signIn(_ request: SignInRequestModel, onSuccess: @escaping() -> Void, onFailure: @escaping(SignInErrorType) -> Void) {
        auth.signIn(username: request.email, password: request.password) { result in
            switch result {
            case .success:
                print("Success signin.")
                onSuccess()
            case .failure(let error):
                print("Fail signIn:", error)
                guard let error = error.underlyingError as? AWSCognitoAuthError else {
                    onFailure(.other) // ★なぜかここが呼ばれる(AWSCognitoAuthErrorでキャストできない)
                    return
                }
                switch error {
                case .userNotFound:
                    onFailure(.userNotFound)
                case .invalidParameter:
                    onFailure(.invalidParameter)
                case .invalidPassword:
                    onFailure(.invalidPassword)
                case .userNotConfirmed:
                    onFailure(.userNotConfirmed)
                default:
                    onFailure(.other)
                }
            }
        }
    }

他の画面(アカウント作成やパスワード再発行等)ではキャストしてエラーハンドリングが出来たのになぜかできない。

調査内容

なぜ、サインイン画面だけキャストできないのか

AuthErrorのunderlyingError
キャストが出来ている画面では、case .serviceに入ってくる。
スクリーンショット 2023-09-29 17.25.31.png
キャストできない画面では、case .notAuthorizedに入ってきていた。
スクリーンショット 2023-09-29 17.25.04.png

なぜ、notAuthorizedになるのか

cognitoの管理画面でユーザー存在エラーを防ぐ設定を有効にしてるかどうかで返ってくるエラーが違うらしい。

有効 ⇒ notAuthorized
無効 ⇒ service

■参考URL
https://github.com/aws-amplify/amplify-swift/issues/1600#issuecomment-1018962055

解決方法

Cognitoの管理画面でユーザー存在エラーを防ぐ設定を無効にする。

■確認手順
cognito⇒ユーザープール⇒対象のユーザープール⇒アプリケーションの統合⇒アプリクライアントと分析
スクリーンショット 2023-09-22 12.45.14.png
高度な認証設定でユーザー存在エラーの防止を有効化になっていたら、編集でチェックを外して無効にする。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1