Skip to content

Commit 5fbe710

Browse files
authored
Adds AuthDataResult to anonymous sign in (#470)
* Adds AuthDataResult to anonymous sign in * Fixes typo * Addresses comments * addresses comment on PR
1 parent 4fbc969 commit 5fbe710

File tree

6 files changed

+158
-9
lines changed

6 files changed

+158
-9
lines changed

Example/Auth/Sample/MainViewController.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@
116116
*/
117117
static NSString *const kSignInAnonymouslyButtonText = @"Sign In Anonymously";
118118

119+
/** @var kSignInAnonymouslyWithAuthResultButtonText
120+
@brief The text of the "Sign In Anonymously (AuthDataResult)" button.
121+
*/
122+
static NSString *const kSignInAnonymouslyWithAuthResultButtonText =
123+
@"Sign In Anonymously (AuthDataResult)";
124+
119125
/** @var kSignedInAlertTitle
120126
@brief The text of the "Sign In Succeeded" alert.
121127
*/
@@ -702,6 +708,8 @@ - (void)updateTable {
702708
action:^{ [weakSelf signInWithCustomToken]; }],
703709
[StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyButtonText
704710
action:^{ [weakSelf signInAnonymously]; }],
711+
[StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyWithAuthResultButtonText
712+
action:^{ [weakSelf signInAnonymouslyAuthDataResult]; }],
705713
[StaticContentTableViewCell cellWithTitle:kGitHubSignInButtonText
706714
action:^{ [weakSelf signInWithGitHub]; }],
707715
[StaticContentTableViewCell cellWithTitle:kSignOutButtonText
@@ -2781,6 +2789,23 @@ - (void)signInAnonymously {
27812789
}];
27822790
}
27832791

2792+
/** @fn signInAnonymouslyAuthDataResult
2793+
@brief Signs in as an anonymous user, receiving an auth result containing a signed in user upon
2794+
success.
2795+
*/
2796+
- (void)signInAnonymouslyAuthDataResult {
2797+
[[AppManager auth] signInAnonymouslyAndRetrieveDataWithCompletion:
2798+
^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) {
2799+
if (error) {
2800+
[self logFailure:@"sign-in anonymously failed" error:error];
2801+
} else {
2802+
[self logSuccess:@"sign-in anonymously succeeded."];
2803+
[self log:[NSString stringWithFormat:@"User ID : %@", authResult.user.uid]];
2804+
}
2805+
[self showTypicalUIForUserUpdateResultsWithTitle:kSignInAnonymouslyButtonText error:error];
2806+
}];
2807+
}
2808+
27842809
/** @fn signInWithGitHub
27852810
@brief Signs in as a GitHub user. Prompts the user for an access token and uses this access
27862811
token to create a GitHub (generic) credential for signing in.

Example/Auth/Tests/FIRAuthTests.m

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ - (void)testSignInWithGoogleCredentialFailure {
10291029
}
10301030

10311031
/** @fn testSignInAnonymouslySuccess
1032-
@brief Tests the flow of a successful @c signInAnonymously:completion: call.
1032+
@brief Tests the flow of a successful @c signInAnonymouslyWithCompletion: call.
10331033
*/
10341034
- (void)testSignInAnonymouslySuccess {
10351035
OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]])
@@ -1061,7 +1061,7 @@ - (void)testSignInAnonymouslySuccess {
10611061
}
10621062

10631063
/** @fn testSignInAnonymouslyFailure
1064-
@brief Tests the flow of a failed @c signInAnonymously:completion: call.
1064+
@brief Tests the flow of a failed @c signInAnonymouslyWithCompletion: call.
10651065
*/
10661066
- (void)testSignInAnonymouslyFailure {
10671067
OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]])
@@ -1081,6 +1081,59 @@ - (void)testSignInAnonymouslyFailure {
10811081
OCMVerifyAll(_mockBackend);
10821082
}
10831083

1084+
/** @fn testSignInAnonymouslyAndRetrieveDataSuccess
1085+
@brief Tests the flow of a successful @c signInAnonymouslyAndRetrieveDataWithCompletion: call.
1086+
*/
1087+
- (void)testSignInAnonymouslyAndRetrieveDataSuccess {
1088+
OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]])
1089+
.andCallBlock2(^(FIRSignUpNewUserRequest *_Nullable request,
1090+
FIRSignupNewUserCallback callback) {
1091+
XCTAssertEqualObjects(request.APIKey, kAPIKey);
1092+
XCTAssertNil(request.email);
1093+
XCTAssertNil(request.password);
1094+
XCTAssertTrue(request.returnSecureToken);
1095+
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
1096+
id mockSignUpNewUserResponse = OCMClassMock([FIRSignUpNewUserResponse class]);
1097+
[self stubTokensWithMockResponse:mockSignUpNewUserResponse];
1098+
callback(mockSignUpNewUserResponse, nil);
1099+
});
1100+
});
1101+
[self expectGetAccountInfoAnonymous];
1102+
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
1103+
[[FIRAuth auth] signOut:NULL];
1104+
[[FIRAuth auth] signInAnonymouslyAndRetrieveDataWithCompletion:
1105+
^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
1106+
XCTAssertTrue([NSThread isMainThread]);
1107+
[self assertUserAnonymous:result.user];
1108+
XCTAssertNil(error);
1109+
[expectation fulfill];
1110+
}];
1111+
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
1112+
[self assertUserAnonymous:[FIRAuth auth].currentUser];
1113+
OCMVerifyAll(_mockBackend);
1114+
}
1115+
1116+
/** @fn testSignInAnonymouslyAndRetrieveDataFailure
1117+
@brief Tests the flow of a failed @c signInAnonymouslyAndRetrieveDataWithCompletion: call.
1118+
*/
1119+
- (void)testSignInAnonymouslyAndRetrieveDataFailure {
1120+
OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]])
1121+
.andDispatchError2([FIRAuthErrorUtils operationNotAllowedErrorWithMessage:nil]);
1122+
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
1123+
[[FIRAuth auth] signOut:NULL];
1124+
[[FIRAuth auth] signInAnonymouslyAndRetrieveDataWithCompletion:
1125+
^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
1126+
XCTAssertTrue([NSThread isMainThread]);
1127+
XCTAssertNil(result);
1128+
XCTAssertEqual(error.code, FIRAuthErrorCodeOperationNotAllowed);
1129+
XCTAssertNotNil(error.userInfo[NSLocalizedDescriptionKey]);
1130+
[expectation fulfill];
1131+
}];
1132+
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
1133+
XCTAssertNil([FIRAuth auth].currentUser);
1134+
OCMVerifyAll(_mockBackend);
1135+
}
1136+
10841137
/** @fn testSignInWithCustomTokenSuccess
10851138
@brief Tests the flow of a successful @c signInWithCustomToken:completion: call.
10861139
*/

Firebase/Auth/Source/FIRAdditionalUserInfo.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ + (nullable instancetype)userInfoWithVerifyAssertionResponse:
5050
isNewUser:verifyAssertionResponse.isNewUser];
5151
}
5252

53-
- (nullable instancetype)initWithProviderID:(NSString *)providerID
53+
- (nullable instancetype)initWithProviderID:(nullable NSString *)providerID
5454
profile:(nullable NSDictionary<NSString *, NSObject *> *)profile
5555
username:(nullable NSString *)username
5656
isNewUser:(BOOL)isNewUser {

Firebase/Auth/Source/FIRAdditionalUserInfo_Internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
3636
@param username The name of the user.
3737
@param isNewUser Indicates whether or not the current user was signed in for the first time.
3838
*/
39-
- (nullable instancetype)initWithProviderID:(NSString *)providerID
39+
- (nullable instancetype)initWithProviderID:(nullable NSString *)providerID
4040
profile:(nullable NSDictionary<NSString *, NSObject *> *)profile
4141
username:(nullable NSString *)username
4242
isNewUser:(BOOL)isNewUser NS_DESIGNATED_INITIALIZER;

Firebase/Auth/Source/FIRAuth.m

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,47 @@ - (void)signInWithCredential:(FIRAuthCredential *)credential
676676
}];
677677
}
678678

679+
- (void)signInAnonymouslyAndRetrieveDataWithCompletion:(FIRAuthDataResultCallback)completion {
680+
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
681+
FIRAuthDataResultCallback decoratedCallback =
682+
[self signInFlowAuthDataResultCallbackByDecoratingCallback:completion];
683+
if (_currentUser.anonymous) {
684+
FIRAdditionalUserInfo *additionalUserInfo =
685+
[[FIRAdditionalUserInfo alloc] initWithProviderID:nil
686+
profile:nil
687+
username:nil
688+
isNewUser:NO];
689+
FIRAuthDataResult *authDataResult =
690+
[[FIRAuthDataResult alloc] initWithUser:_currentUser
691+
additionalUserInfo:additionalUserInfo];
692+
decoratedCallback(authDataResult, nil);
693+
return;
694+
}
695+
[self internalSignInAnonymouslyWithCompletion:^(FIRSignUpNewUserResponse *_Nullable response,
696+
NSError *_Nullable error) {
697+
if (error) {
698+
decoratedCallback(nil, error);
699+
return;
700+
}
701+
[self completeSignInWithAccessToken:response.IDToken
702+
accessTokenExpirationDate:response.approximateExpirationDate
703+
refreshToken:response.refreshToken
704+
anonymous:YES
705+
callback:^(FIRUser *_Nullable user, NSError *_Nullable error) {
706+
FIRAdditionalUserInfo *additionalUserInfo =
707+
[[FIRAdditionalUserInfo alloc] initWithProviderID:nil
708+
profile:nil
709+
username:nil
710+
isNewUser:YES];
711+
FIRAuthDataResult *authDataResult =
712+
[[FIRAuthDataResult alloc] initWithUser:user
713+
additionalUserInfo:additionalUserInfo];
714+
decoratedCallback(authDataResult, nil);
715+
}];
716+
}];
717+
});
718+
}
719+
679720
- (void)signInAnonymouslyWithCompletion:(FIRAuthResultCallback)completion {
680721
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
681722
FIRAuthResultCallback decoratedCallback =
@@ -684,11 +725,8 @@ - (void)signInAnonymouslyWithCompletion:(FIRAuthResultCallback)completion {
684725
decoratedCallback(_currentUser, nil);
685726
return;
686727
}
687-
FIRSignUpNewUserRequest *request =
688-
[[FIRSignUpNewUserRequest alloc]initWithRequestConfiguration:_requestConfiguration];
689-
[FIRAuthBackend signUpNewUser:request
690-
callback:^(FIRSignUpNewUserResponse *_Nullable response,
691-
NSError *_Nullable error) {
728+
[self internalSignInAnonymouslyWithCompletion:^(FIRSignUpNewUserResponse *_Nullable response,
729+
NSError *_Nullable error) {
692730
if (error) {
693731
decoratedCallback(nil, error);
694732
return;
@@ -1092,6 +1130,16 @@ - (void)phoneNumberSignInWithRequest:(FIRVerifyPhoneNumberRequest *)request
10921130
}
10931131
#endif
10941132

1133+
/** @fn internalSignInAnonymouslyWithCompletion:
1134+
@param completion A block which is invoked when the anonymous sign in request completes.
1135+
*/
1136+
- (void)internalSignInAnonymouslyWithCompletion:(FIRSignupNewUserCallback)completion {
1137+
FIRSignUpNewUserRequest *request =
1138+
[[FIRSignUpNewUserRequest alloc]initWithRequestConfiguration:_requestConfiguration];
1139+
[FIRAuthBackend signUpNewUser:request
1140+
callback:completion];
1141+
}
1142+
10951143
/** @fn possiblyPostAuthStateChangeNotification
10961144
@brief Posts the auth state change notificaton if current user's token has been changed.
10971145
*/

Firebase/Auth/Source/Public/FIRAuth.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,29 @@ FIR_SWIFT_NAME(Auth)
408408
*/
409409
- (void)signInAnonymouslyWithCompletion:(nullable FIRAuthResultCallback)completion;
410410

411+
/** @fn signInAnonymouslyAndRetrieveDataWithCompletion:
412+
@brief Asynchronously creates and becomes an anonymous user.
413+
@param completion Optionally; a block which is invoked when the sign in finishes, or is
414+
canceled. Invoked asynchronously on the main thread in the future.
415+
416+
@remarks If there is already an anonymous user signed in, that user will be returned instead.
417+
If there is any other existing user signed in, that user will be signed out.
418+
419+
@remarks Possible error codes:
420+
<ul>
421+
<li>@c FIRAuthErrorCodeOperationNotAllowed - Indicates that anonymous accounts are
422+
not enabled. Enable them in the Auth section of the Firebase console.
423+
</li>
424+
</ul>
425+
426+
@remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
427+
@remarks This method will only exist until the next major Firebase release following 4.x.x.
428+
After the next major release the method @c signInAnonymouslyWithCompletion will support the
429+
@c FIRAuthDataResultCallback.
430+
*/
431+
- (void)signInAnonymouslyAndRetrieveDataWithCompletion:
432+
(nullable FIRAuthDataResultCallback)completion;
433+
411434
/** @fn signInWithCustomToken:completion:
412435
@brief Asynchronously signs in to Firebase with the given Auth token.
413436

0 commit comments

Comments
 (0)