Reading Firebase Auth Error Thrown (Firebase 3.x and Swift)
Try this. This works for me. Also, after pasting this into your project. If you need to see all the FIRAuthErrorCode
codes. Hover your mouse over .ErrorCodeInvalidEmail
then press your left mouse button and it will show you the rest.
If you have any problems let me know and ill try to help you. Good luck!
FIRAuth.auth()?.createUserWithEmail(email, password: password) { (user, error) in
if error != nil {
if let errCode = FIRAuthErrorCode(rawValue: error!._code) {
switch errCode {
case .ErrorCodeInvalidEmail:
print("invalid email")
case .ErrorCodeEmailAlreadyInUse:
print("in use")
default:
print("Create User Error: \(error!)")
}
}
} else {
print("all good... continue")
}
}
Firebase 3.6.0 (Auth) - Detecting the specific error using Swift 3.0
Use this:-
if let errCode = FIRAuthErrorCode(rawValue: err!._code) {
switch errCode {
case .errorCodeInvalidEmail:
print("invalid email")
case .errorCodeEmailAlreadyInUse:
print("in use")
default:
print("Other error!")
}
}
Where err is the received error from firebase
Handling Errors in New Firebase and Swift
I've actually just struggled with this for quite a bit of time and found what the issue was. I've tried the code posted in an answer above and the error.code line gave me an error. It did work with error._code though. In other words, credit for the original answer to Paul with a slight modificaiton. Here's my final code (I will edit it for all errors though):
if let errCode = AuthErrorCode(rawValue: error!._code) {
switch errCode {
case .errorCodeInvalidEmail:
print("invalid email")
case .errorCodeEmailAlreadyInUse:
print("in use")
default:
print("Create User Error: \(error)")
}
}
SwiftUI Firebase How To Do Custom Error Handling
All of the Authentication error codes are listed in the Authentication Documentation.
Here's a quick snippet of how to handle the errors and present your own error message.
Auth.auth().signIn....() { (auth, error) in //some signIn function
if let x = error {
let err = x as NSError
switch err.code {
case AuthErrorCode.wrongPassword.rawValue:
print("wrong password, you big dummy")
case AuthErrorCode.invalidEmail.rawValue:
print("invalid email - duh")
case AuthErrorCode.accountExistsWithDifferentCredential.rawValue:
print("the account already exists")
default:
print("unknown error: \(err.localizedDescription)")
}
} else {
if let _ = auth?.user {
print("authd")
} else {
print("no authd user")
}
}
}
There are many ways to code this so this is just an example.
How to handle Firebase Auth Errors? / Swift / Firebase
You can bridge to NSError
and then create the AuthErrorCode
based on error.code
:
Auth.auth().createUser(withEmail: "MyEmail", password: "MyPassword") { authResult, error in
if error != nil, let error = error as NSError? {
if let errorCode = AuthErrorCode(rawValue: error.code) {
switch errorCode {
case .invalidEmail:
break
case .emailAlreadyInUse:
break
default:
break
}
}
} else {
//no error
}
}
Note that I only listed a couple of the errorCode
possibilities - there's quite an extensive list of them.
Flutter network not connecting to FirebaseAuth services
I had this issue because I was running an Android app on an emulator, so I added the line of code below. and after running the app on a real device using the same code, I started having problems.
FirebaseAuth.instance.useAuthEmulator("localhost", 9099);
Remove above line if you are running app in real device
How to Handle Firebase Auth exceptions on flutter
(21/02/20) EDIT: This answer is old and the other answers contains cross platform solutions so you should look at theirs first and treat this as a fallback solution.
The firebase auth plugin doesn't really have a proper cross-platform error code system yet so you have to handle errors for android and ios independently.
I'm currently using the temporary fix from this github issue: #20223
Do note since its a temp fix, don't expect it to be fully reliable as a permanent solution.
enum authProblems { UserNotFound, PasswordNotValid, NetworkError }
try {
FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
} catch (e) {
authProblems errorType;
if (Platform.isAndroid) {
switch (e.message) {
case 'There is no user record corresponding to this identifier. The user may have been deleted.':
errorType = authProblems.UserNotFound;
break;
case 'The password is invalid or the user does not have a password.':
errorType = authProblems.PasswordNotValid;
break;
case 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.':
errorType = authProblems.NetworkError;
break;
// ...
default:
print('Case ${e.message} is not yet implemented');
}
} else if (Platform.isIOS) {
switch (e.code) {
case 'Error 17011':
errorType = authProblems.UserNotFound;
break;
case 'Error 17009':
errorType = authProblems.PasswordNotValid;
break;
case 'Error 17020':
errorType = authProblems.NetworkError;
break;
// ...
default:
print('Case ${e.message} is not yet implemented');
}
}
print('The error is $errorType');
}
Related Topics
Change Table to Edit Mode and Delete Rows Inside a Normal Viewcontroller
Bridge Google Drive API to Swift
Width and Height Equal to Its Superview Using Autolayout Programmatically
How to Use Multi-Path Update with the Rest API in Firebase? Error 400
@State Var Not Updated as Expected in Lazyvgrid
Cannot Assign Value of Type '() -> Void' to Type '(() -> Void)!'
Why My Nsdateformatter Returns Null
iOS Simulator Display Scaled Incorrectly
iOS Bar Item Image Displaying Wrong Color
How to Delete Object from Core Data in Swift 3
How to Return Value After the Execution of the Block? Swift
How to Upload Audio with Alamofire Multipart Upload
Return Lighter Color from Skcolor Using Hsl Lightness Factor
How to Cache Using Nsurlsession and Nsurlcache. Not Working
iOS 8 Photos Framework. Access Photo Metadata
How to Track User Location in Background
How to Convert String to Date to String in Swift iOS
Populating Tableview with Multiple Sections and Multiple Dictionary in an Array in Swift