Search code examples
iosemail-validationfirebase

Validating emails with firebase for iOS


When submitting a request to create a new user, if the user tries to submit with an invalid email, the app crashes (I.E. without an @ sign). Is there a way to check if an email is valid through Firebase or is that something I need to validate app side?

The error I get in the console is:

* Terminating app due to uncaught exception 'InvalidEmail', reason: 'jfkdsla;fjdk is an invalid email address'

NSString *url = [NSString stringWithFormat:@"https://myapp.firebaseio.com/users/%@", displayName.text];
    Firebase *dataRef = [[Firebase alloc] initWithUrl:url];
    [dataRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

        NSLog(@"Ref %@", snapshot.value);
        snapshotValue = snapshot.value;
    }];

    if (snapshotValue == NULL) {
        AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];

        [appDelegate.authClient createUserWithEmail:email.text password:password.text andCompletionBlock:^(NSError* error, FAUser*user) {

            if (error != nil) {
                // Error
                NSLog(@"ERROR CODE: %d",error.code);
                NSLog(@"ERROR: %@",error);

                if (error.code == -9999) {

                    email.textColor = [UIColor redColor];

                }

            } else {

                NSLog(@"Created %@",user.userId);
                Firebase *userRef = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"https://myapp.firebaseio.com/users/%@",displayName.text]];
                [[userRef childByAppendingPath:@"name"] setValue:fullName.text];
                [[userRef childByAppendingPath:@"user_id"] setValue:user.userId];

                Firebase *userCredentials = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"https://myapp.firebaseio.com/users/%@/credentials",displayName.text]];
                [[userCredentials childByAppendingPath:@"email"] setValue:email.text];
                [[userCredentials childByAppendingPath:@"password"] setValue:password.text];



                AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];

                [appDelegate.authClient loginWithEmail:email.text andPassword:password.text
                                   withCompletionBlock:^(NSError* error, FAUser* user) {

                                       if (error != nil) {
                                           NSLog(@"There was an error logging in to this account: %@",error);

                                       } else {
                                           NSLog(@"logged in");
                                           [self dismissViewControllerAnimated:NO completion:nil];

                                           [appDelegate.window.rootViewController presentViewController:appDelegate.tabBarController animated:YES completion:nil];

                                       }
                                   }];                    

            }

        }];

    }
    else {

        displayName.textColor = [UIColor redColor];

    }

    }

Solution

  • Since an exception is being thrown, throw the "createUserWithEmail" line of code into a "@try{} / @catch{}" block:

    @try {
        [appDelegate.authClient createUserWithEmail:email.text password:password.text andCompletionBlock:^(NSError* error, FAUser*user) {
            ...
            ...
            // keep that big huge block part intact
            ...
            ...
        }
    }
    @catch (NSException * e) {
            NSLog(@"Exception: %@", e);
        }
    }