ios,swift2020. 3. 18. 10:19

[ios]swift firebase Auth error code 확인하는방법

 

파이어베이스로 이메일 회원가입을 시도하려고 한다.

 

자 그러면 회원가입을 하는데 이미 회원가입이 되있으면 error처리를 해야 한다.. 

 

그런데 error안에 domain , code가 있다는걸 볼 수 있다. 

 

근데 이걸 어떻게 뽑아내냐?? 

 

두가지 방법이 있다.

 

Auth.auth().createUser(withEmail: email, password:password) { (authResult, error) in

            // error

            guard let user = authResult?.user  , error == nil else

            {

                // 1 이렇게 해야 한다. 권장

                if let errorCode : AuthErrorCode = AuthErrorCode(rawValue: error!._code)

                {

                    print("-> errorCode -> \(errorCode.rawValue)")

                    

                    if AuthErrorCode.emailAlreadyInUse.rawValue == errorCode.rawValue

                    {

                        

                    }

                }

                

                // 2 권장하지 않는 사항

                // 인증 메소드의 완료 콜백이 값이 nil이 아닌 NSError 인수를 받으면 오류가 발생한 것입니다.

                // 제품 코드에서 이 인수를 적절한 오류 처리 로직으로 전달하려면

                // 오류 코드를 아래의 공통 오류 및 메소드별 오류 목록과 대조하여 확인해야 합니다.

                let er : NSError = error as! NSError

                print("-> er -> \(er.description) , code -> \(er.code)")

                if er.code == AuthErrorCode.expiredActionCode.rawValue

                {

                    

                }

                

                

                print("-> eror-> \(error.debugDescription)")

                return

            }

        }

 

처음에는 NSError했더니.. 문서상에서 권장하지 않는단다..

 

그러면 AuthErrorCode(rawValue: error!._code) 하면 된다.

 

 

 

[참조1] https://firebase.google.com/docs/auth/ios/errors

 

Firebase iOS 인증 오류 처리하기

인증 메소드의 완료 콜백이 값이 nil이 아닌 NSError 인수를 받으면 오류가 발생한 것입니다. 제품 코드에서 이 인수를 적절한 오류 처리 로직으로 전달하려면 오류 코드를 아래의 공통 오류 및 메소드별 오류 목록과 대조하여 확인해야 합니다. 일부 오류는 특정한 사용자 조치를 통해 해결할 수 있습니다. 예를 들어 FIRAuthErrorCodeUserTokenExpired는 사용자를 다시 로그인 처리하면 해결할 수 있고 FIRAuthErrorCodeWro

firebase.google.com

[참조2] https://stackoverflow.com/questions/37449919/reading-firebase-auth-error-thrown-firebase-3-x-and-swift

 

Reading Firebase Auth Error Thrown (Firebase 3.x and Swift)

I'm having trouble figuring out how to read the FIRAuthErrorNameKey in the new version of Firebase. The following is what I have so far, but the "let errorCode = FIRAuthErrorNameKey" line is incor...

stackoverflow.com

 

Posted by thdeodls85