Skip to content

Commit 439c291

Browse files
authored
Handle missing email error (#54)
Adds appropriate error handling for missing email in the createUserWithEmail:password:completion: flow. Also fixes a few typos.
1 parent 51ed6b0 commit 439c291

File tree

7 files changed

+55
-4
lines changed

7 files changed

+55
-4
lines changed

Example/Auth/Tests/FIRAuthTests.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,25 @@ - (void)testCreateUserEmptyPasswordFailure {
12001200
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
12011201
}
12021202

1203+
/** @fn testCreateUserEmptyEmailFailure
1204+
@brief Tests the flow of a failed @c createUserWithEmail:password:completion: call due to an
1205+
empty email adress. This error occurs on the client side, so there is no need to fake an RPC
1206+
response.
1207+
*/
1208+
- (void)testCreateUserEmptyEmailFailure {
1209+
[self expectGetAccountInfo];
1210+
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
1211+
[[FIRAuth auth] signOut:NULL];
1212+
[[FIRAuth auth] createUserWithEmail:@""
1213+
password:kPassword
1214+
completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
1215+
XCTAssertTrue([NSThread isMainThread]);
1216+
XCTAssertEqual(error.code, FIRAuthErrorCodeMissingEmail);
1217+
[expectation fulfill];
1218+
}];
1219+
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
1220+
}
1221+
12031222
/** @fn testSendPasswordResetEmailSuccess
12041223
@brief Tests the flow of a successful @c sendPasswordResetWithEmail:completion: call.
12051224
*/

Example/Auth/Tests/FIRSignUpNewUserResponseTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ - (void)testSignUpNewUserPasswordLoginDisabledError {
242242
XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
243243
}
244244

245-
/** @fn testinvalidEmailError
245+
/** @fn testInvalidEmailError
246246
@brief This test simulates making a request containing an invalid email address and receiving @c
247247
FIRAuthErrorInvalidEmail error as a result.
248248
*/
249-
- (void)testinvalidEmailError {
249+
- (void)testInvalidEmailError {
250250
FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
251251

252252
__block BOOL callbackInvoked;

Firebase/Auth/Source/FIRAuth.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
/** @var kMissingEmailInvalidParameterExceptionReason
9494
@brief The key of missing email key @c invalidParameterException.
9595
*/
96-
static NSString *const kEmailInvalidParameterReason = @"The email used to initiate user password "
96+
static NSString *const kEmailInvalidParameterReason = @"The email used to initiate password reset "
9797
"cannot be nil";
9898

9999
static NSString *const kPasswordResetRequestType = @"PASSWORD_RESET";
@@ -650,6 +650,10 @@ - (void)createUserWithEmail:(NSString *)email
650650
weakPasswordErrorWithServerResponseReason:kMissingPasswordReason]);
651651
return;
652652
}
653+
if (![request.email length]) {
654+
decoratedCallback(nil, [FIRAuthErrorUtils missingEmail]);
655+
return;
656+
}
653657
[FIRAuthBackend signUpNewUser:request
654658
callback:^(FIRSignUpNewUserResponse *_Nullable response,
655659
NSError *_Nullable error) {

Firebase/Auth/Source/FIRAuthErrorUtils.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@
217217
static NSString *const kFIRAuthErrorMessageInvalidRecipientEmail = @"The action code is invalid. "
218218
"This can happen if the code is malformed, expired, or has already been used.";
219219

220+
/** @var kFIRAuthErrorMessageMissingEmail
221+
@brief Message for @c FIRAuthErrorCodeMissingEmail error code.
222+
*/
223+
static NSString *const kFIRAuthErrorMessageMissingEmail = @"An email address must be provided.";
224+
220225
/** @var kFIRAuthErrorMessageMissingContinueURI
221226
@brief Message for @c FIRAuthErrorCodeMissingContinueURI error code.
222227
*/
@@ -381,6 +386,8 @@
381386
return kFIRAuthErrorMessageInvalidMessagePayload;
382387
case FIRAuthErrorCodeInvalidRecipientEmail:
383388
return kFIRAuthErrorMessageInvalidRecipientEmail;
389+
case FIRAuthErrorCodeMissingEmail:
390+
return kFIRAuthErrorMessageMissingEmail;
384391
case FIRAuthErrorCodeMissingPhoneNumber:
385392
return kFIRAuthErrorMessageMissingPhoneNumber;
386393
case FIRAuthErrorCodeInvalidPhoneNumber:
@@ -474,6 +481,8 @@
474481
return @"ERROR_INVALID_SENDER";
475482
case FIRAuthErrorCodeInvalidRecipientEmail:
476483
return @"ERROR_INVALID_RECIPIENT_EMAIL";
484+
case FIRAuthErrorCodeMissingEmail:
485+
return @"MISSING_EMAIL";
477486
case FIRAuthErrorCodeMissingPhoneNumber:
478487
return @"ERROR_MISSING_PHONE_NUMBER";
479488
case FIRAuthErrorCodeInvalidPhoneNumber:
@@ -731,6 +740,10 @@ + (NSError *)invalidRecipientEmailErrorWithMessage:(nullable NSString *)message
731740
return [self errorWithCode:FIRAuthInternalErrorCodeInvalidRecipientEmail message:message];
732741
}
733742

743+
+ (NSError *)missingEmail {
744+
return [self errorWithCode:FIRAuthInternalErrorCodeMissingEmail];
745+
}
746+
734747
+ (NSError *)missingPhoneNumberErrorWithMessage:(nullable NSString *)message {
735748
return [self errorWithCode:FIRAuthInternalErrorCodeMissingPhoneNumber message:message];
736749
}

Firebase/Auth/Source/FIRAuthErrors.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ typedef NS_ENUM(NSInteger, FIRAuthErrorCode) {
184184
*/
185185
FIRAuthErrorCodeInvalidRecipientEmail = 17033,
186186

187-
// The enum values between 17033 and 17041 are reserved and should NOT be used for new error
187+
/** Indicates that an email address was expected but one was not provided.
188+
*/
189+
FIRAuthErrorCodeMissingEmail = 17034,
190+
191+
// The enum values between 17034 and 17041 are reserved and should NOT be used for new error
188192
// codes.
189193

190194
/** Indicates that a phone number was not provided in a call to

Firebase/Auth/Source/Private/FIRAuthErrorUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ NS_ASSUME_NONNULL_BEGIN
313313
*/
314314
+ (NSError *)invalidRecipientEmailErrorWithMessage:(nullable NSString *)message;
315315

316+
/** @fn missingEmail
317+
@brief Constructs an @c NSError with the @c FIRAuthErrorCodeMissingEmail code.
318+
@return The NSError instance associated with the given FIRAuthError.
319+
*/
320+
+ (NSError *)missingEmail;
321+
316322
/** @fn missingPhoneNumberErrorWithMessage:
317323
@brief Constructs an @c NSError with the @c FIRAuthErrorCodeMissingPhoneNumber code.
318324
@param message Error message from the backend, if any.

Firebase/Auth/Source/Private/FIRAuthInternalErrors.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ typedef NS_ENUM(NSInteger, FIRAuthInternalErrorCode) {
238238
FIRAuthInternalErrorCodeInvalidRecipientEmail =
239239
FIRAuthPublicErrorCodeFlag | FIRAuthErrorCodeInvalidRecipientEmail,
240240

241+
/** Indicates that an email address was expected but one was not provided.
242+
*/
243+
FIRAuthInternalErrorCodeMissingEmail =
244+
FIRAuthPublicErrorCodeFlag | FIRAuthErrorCodeMissingEmail,
245+
241246
/** Indicates that a phone number was not provided in a call to @c verifyPhoneNumber:completion:.
242247
*/
243248
FIRAuthInternalErrorCodeMissingPhoneNumber =

0 commit comments

Comments
 (0)