Not sure if this a bug or an intended feature.
To create a user with an email and password in Firebase, I've been using the following code:
FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
if let error = error {
guard let error = error as? FIRAuthErrorCode else { return } // ALWAYS FAILS
...code...
}
...code...
}
The error parameter in the completion handler for the method cannot be cast as FIRAuthErrorCode; it always fails. Is that a bug, or is that the expected behaviour?
Edit: I am aware that error codes can be used to distinguish between the different types of FIRAuthErrorCode errors. It's just not readable, and it doesn't make much sense for the error parameter in the completion handler to be not of be of type FIRAuthErrorCode. The cases and error codes of FIRAuthErrorCode can be found here.
Have you tried using guard let error = error as! FIRAuthErrorCode else { return } to force the casting and check whether the return is nil or not?
After contacting Firebase support about the issue, they've said that the errors that are passed back in completion handlers are just Error objects. They weren't FIRAuthErrorCode objects. To test for the various FIRAuthErrorCode cases, one would have to do something like this:
FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
if let error = error {
guard let error = FIRAuthErrorCode(rawValue: error._code) else {
fatalError("This should never be executed")
}
switch error {
case .errorCodeInvalidEmail: ...
case .errorCodeWrongPassword: ...
default: ...
}
...code...
}
...code...
}
^ This preserves readability and makes error handling more intuitive. And, there is no need for error casting!
You should check the documentation of signIn method in firebase in order to check the all possible type of error this method can send and then check those type of errors in guard block in your code.
Try this, it's how I do my login and it seems to work great.
FIRAuth.auth()?.signIn(withEmail: EmailField.text!, password: PasswordField.text!, completion: { user, error in
if error == nil {
print("Successfully Logged IN \(user!)")
self.performSegue(withIdentifier: "Login", sender: self)
}
})
I just check that there's not an error with the signin process, then perform my segue.
Related
I am attempting to implement amplify auth on iOS, and what I would like to be able to do is customize the error message that is displayed to a user when authentication fails, as the default error messages are not end-user friendly, but I have no idea how to do this.
For instance, my signIn method is as follows:
func signIn(username: String) {
Amplify.Auth.signIn(username: username, password: "bla") { [weak self] result in
switch result {
case .success (let result):
if case .confirmSignInWithCustomChallenge(_) = result.nextStep {
DispatchQueue.main.async {
self?.showConfirmationSignInView()
}
} else {
print("Sign in succeeded")
}
case .failure(let error):
print (error)
}
}
}
Now in the .failure case, instead of printing the error, I would ideally like to determine if the error is a userNotFound error, or something else. I can't find any info in the docs on this. Any help would be appreciated.
You can do it by checking the error.code. for example, for a user who did not confirm the email if he tries to login then error.code will have UserNotConfirmedException string value. Amplify auth returns different exception codes for different types of errors. You can see all the exceptions from this link. Although it is for flutter, the exception code is identical for any framework. I have used these exception codes in react.
when i run the following code i get this error.
['FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but User has 1']
func saveLabel(uid: String, label: String){
let userRef = db.collection("User").document(uid)
userRef.updateData(["label": label]) { (error) in
if error == nil {
print("updated")
}else{
print("not updated")
}
}
}
This error almost certainly means that uid is empty. You should log it to make sure.
I'm currently running into this error when implementing the AWSMobileClient signUp function. I haven't really altered the code sample from the AWS page describing how to implement it, other than changing the attributes to fit my user pool attribute requirements.
First in viewDidLoad, I initialize the mobile client like so:
AWSMobileClient.sharedInstance().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState.rawValue)")
} else if let error = error {
print("error: \(error.localizedDescription)")
}
}
Then I have the function for signing up. This is what the code looks like (I encapsulate this in a function called signUpUser):
AWSMobileClient.sharedInstance().signUp(username: userEmail,
password: userPass,
userAttributes: ["email":userEmail, "given_name":userFirstName, "family_name": userLastName, "custom:school":userSchool]) { (signUpResult, error) in
if let signUpResult = signUpResult {
switch(signUpResult.signUpConfirmationState) {
case .confirmed:
print("User is signed up and confirmed.")
case .unconfirmed:
print("User is not confirmed and needs verification via \(signUpResult.codeDeliveryDetails!.deliveryMedium) sent at \(signUpResult.codeDeliveryDetails!.destination!)")
case .unknown:
print("Unexpected case")
}
} else if let error = error {
if let error = error as? AWSMobileClientError {
switch(error) {
case .usernameExists(let message):
print(message)
default:
break
}
}
print("\(error.localizedDescription)")
}
When I run the app on my iPhone, I call this function when the "Sign Up Button" is clicked. In the debug window, I get the following error:
The operation couldn’t be completed. (AWSMobileClient.AWSMobileClientError error 20.)
That's the only info that appears in the Xcode console. Does anyone know how to go about debugging or fixing this?
EDIT: I'm not sure what the issue was that caused this error. I started a fresh project, set up a new cognito pool and backend services, and ported over the code from this project, which resulted in everything working perfectly. The error may have been from incorrectly setting up the user pool, or perhaps not allowing unauthorized access to the sign up function (not sure if I had that set to "No").
If you exhaust the rest of the switch case there, you will be able to see what exactly is the error coming back from the service.
reference: https://stackoverflow.com/a/59521025/2464632
Suppose I am using a code snippet in setup() method just before running a test case like this:
if EarlGrey().selectElementWithMatcher(grey_accessibilityID("TabBar-Navigation-Search")).assertWithMatcher(grey_sufficientlyVisible()) != nil {
TabBarNavigation().navigateToSearch()
} else {
assentViewModelsSwifts.signInCheck()
assentViewModelsSwifts.enterLoginCredentials("username", password: "password")
let visibleSignInButtonMatcher = grey_allOfMatchers(grey_accessibilityID("Login-Button-SignIn"), grey_sufficientlyVisible())
EarlGrey().selectElementWithMatcher(visibleSignInButtonMatcher).performAction(grey_tap())
TabBarNavigation().navigateToSearch()
Log.info("Landed on Search Page")
}
I am running into an error.
Assertion 'assertWithMatcher: matcherForSufficientlyVisible(>=0.750000)' was not performed because no UI element matching (respondsToSelector(accessibilityIdentifier) && accessibilityID("TabBar-Navigation-Activity")) was found.
Actually I want to check the presence of an element. If element is present, do a certain step or do something else. But although the element is not present on that particular screen, the code inside the loop is not executed.
Any suggestion would be appreciated.
Try below method for nil check:
- (instancetype)assertWithMatcher:(id<GREYMatcher>)matcher error:(__strong NSError **)errorOrNil
Try below code to check nil:
let errorOrNil
EarlGrey().selectElementWithMatcher(grey_accessibilityID("TabBar-Navigation-Search")).assertWithMatcher(grey_sufficientlyVisible(), error:&errorOrNil)
if errorOrNil != nil {
TabBarNavigation().navigateToSearch()
} else {
assentViewModelsSwifts.signInCheck()
assentViewModelsSwifts.enterLoginCredentials("username", password: "password")
let visibleSignInButtonMatcher = grey_allOfMatchers(grey_accessibilityID("Login-Button-SignIn"), grey_sufficientlyVisible())
EarlGrey().selectElementWithMatcher(visibleSignInButtonMatcher).performAction(grey_tap())
TabBarNavigation().navigateToSearch()
Log.info("Landed on Search Page")
}
I am new to swift as well as programming but I am trying to retrieve check if a user can log in and I believe I did what Parse recommends to do to do so however I am receiving this error and am unsure as to why.
Here is my code
PFUser.logInWithUsernameInBackground(usernameTextField.text!, password: passwordTextField.text!){
(user: PFUser!, error: NSError) -> Void in
if user != nil {
//Yes User Exists
self.messageLabel.text = "User Exists"
}
else {
//no user doesnt exist
}
}
I updated the xcode for version 8.1 and Parse started giving the same error.
I changed the call to the method to return to work. Here's how I did here:
PFUser.logInWithUsername (inbackground: usernameTextField.text !, password: passwordTextField.text !, block: {(user, error) in
if user != nil {
//Yes User Exists
self.messageLabel.text = "User Exists"
}
else {
//no user doesnt exist
}
})