Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds AuthDataResult to anonymous sign in #470

Merged
merged 4 commits into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Example/Auth/Sample/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
*/
static NSString *const kSignInAnonymouslyButtonText = @"Sign In Anonymously";

/** @var kSignInAnonymouslyWithAuthResultButtonText
@brief The text of the "Sign In Anonymously (AuthDataResult)" button.
*/
static NSString *const kSignInAnonymouslyWithAuthResultButtonText =
@"Sign In Anonymously (AuthDataResult)";

/** @var kSignedInAlertTitle
@brief The text of the "Sign In Succeeded" alert.
*/
Expand Down Expand Up @@ -707,6 +713,8 @@ - (void)updateTable {
action:^{ [weakSelf signInWithCustomToken]; }],
[StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyButtonText
action:^{ [weakSelf signInAnonymously]; }],
[StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyWithAuthResultButtonText
action:^{ [weakSelf signInAnonymouslyAuthDataResult]; }],
[StaticContentTableViewCell cellWithTitle:kGitHubSignInButtonText
action:^{ [weakSelf signInWithGitHub]; }],
[StaticContentTableViewCell cellWithTitle:kSignOutButtonText
Expand Down Expand Up @@ -2786,6 +2794,23 @@ - (void)signInAnonymously {
}];
}

/** @fn signInAnonymouslyAuthDataResult
@brief Signs in as an anonymous user, receiving an auth result containing a signed in user upon
success.
*/
- (void)signInAnonymouslyAuthDataResult {
[[AppManager auth] signInAnonymouslyAndRetrieveDataWithCompletion:
^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) {
if (error) {
[self logFailure:@"sign-in anonymously failed" error:error];
} else {
[self logSuccess:@"sign-in anonymously succeeded."];
[self log:[NSString stringWithFormat:@"User ID : %@", authResult.user.uid]];
}
[self showTypicalUIForUserUpdateResultsWithTitle:kSignInAnonymouslyButtonText error:error];
}];
}

/** @fn signInWithGitHub
@brief Signs in as a GitHub user. Prompts the user for an access token and uses this access
token to create a GitHub (generic) credential for signing in.
Expand Down
57 changes: 55 additions & 2 deletions Example/Auth/Tests/FIRAuthTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ - (void)testSignInWithGoogleCredentialFailure {
}

/** @fn testSignInAnonymouslySuccess
@brief Tests the flow of a successful @c signInAnonymously:completion: call.
@brief Tests the flow of a successful @c signInAnonymouslyWithCompletion: call.
*/
- (void)testSignInAnonymouslySuccess {
OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]])
Expand Down Expand Up @@ -1061,7 +1061,7 @@ - (void)testSignInAnonymouslySuccess {
}

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

/** @fn testSignInAnonymouslyAndRetrieveDataSuccess
@brief Tests the flow of a successful @c signInAnonymouslyAndRetrieveDataWithCompletion: call.
*/
- (void)testSignInAnonymouslyAndRetrieveDataSuccess {
OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]])
.andCallBlock2(^(FIRSignUpNewUserRequest *_Nullable request,
FIRSignupNewUserCallback callback) {
XCTAssertEqualObjects(request.APIKey, kAPIKey);
XCTAssertNil(request.email);
XCTAssertNil(request.password);
XCTAssertTrue(request.returnSecureToken);
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
id mockSignUpNewUserResponse = OCMClassMock([FIRSignUpNewUserResponse class]);
[self stubTokensWithMockResponse:mockSignUpNewUserResponse];
callback(mockSignUpNewUserResponse, nil);
});
});
[self expectGetAccountInfoAnonymous];
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
[[FIRAuth auth] signOut:NULL];
[[FIRAuth auth] signInAnonymouslyAndRetrieveDataWithCompletion:
^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
XCTAssertTrue([NSThread isMainThread]);
[self assertUserAnonymous:result.user];
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
[self assertUserAnonymous:[FIRAuth auth].currentUser];
OCMVerifyAll(_mockBackend);
}

/** @fn testSignInAnonymouslyAndRetrieveDataFailure
@brief Tests the flow of a failed @c signInAnonymouslyAndRetrieveDataWithCompletion: call.
*/
- (void)testSignInAnonymouslyAndRetrieveDataFailure {
OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]])
.andDispatchError2([FIRAuthErrorUtils operationNotAllowedErrorWithMessage:nil]);
XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
[[FIRAuth auth] signOut:NULL];
[[FIRAuth auth] signInAnonymouslyAndRetrieveDataWithCompletion:
^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
XCTAssertTrue([NSThread isMainThread]);
XCTAssertNil(result);
XCTAssertEqual(error.code, FIRAuthErrorCodeOperationNotAllowed);
XCTAssertNotNil(error.userInfo[NSLocalizedDescriptionKey]);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
XCTAssertNil([FIRAuth auth].currentUser);
OCMVerifyAll(_mockBackend);
}

/** @fn testSignInWithCustomTokenSuccess
@brief Tests the flow of a successful @c signInWithCustomToken:completion: call.
*/
Expand Down
2 changes: 1 addition & 1 deletion Firebase/Auth/Source/FIRAdditionalUserInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ + (nullable instancetype)userInfoWithVerifyAssertionResponse:
isNewUser:verifyAssertionResponse.isNewUser];
}

- (nullable instancetype)initWithProviderID:(NSString *)providerID
- (nullable instancetype)initWithProviderID:(nullable NSString *)providerID
profile:(nullable NSDictionary<NSString *, NSObject *> *)profile
username:(nullable NSString *)username
isNewUser:(BOOL)isNewUser {
Expand Down
2 changes: 1 addition & 1 deletion Firebase/Auth/Source/FIRAdditionalUserInfo_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
@param username The name of the user.
@param isNewUser Indicates whether or not the current user was signed in for the first time.
*/
- (nullable instancetype)initWithProviderID:(NSString *)providerID
- (nullable instancetype)initWithProviderID:(nullable NSString *)providerID
profile:(nullable NSDictionary<NSString *, NSObject *> *)profile
username:(nullable NSString *)username
isNewUser:(BOOL)isNewUser NS_DESIGNATED_INITIALIZER;
Expand Down
58 changes: 53 additions & 5 deletions Firebase/Auth/Source/FIRAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,47 @@ - (void)signInWithCredential:(FIRAuthCredential *)credential
}];
}

- (void)signInAnonymouslyAndRetrieveDataWithCompletion:(FIRAuthDataResultCallback)completion {
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
FIRAuthDataResultCallback decoratedCallback =
[self signInFlowAuthDataResultCallbackByDecoratingCallback:completion];
if (_currentUser.anonymous) {
FIRAdditionalUserInfo *additionalUserInfo =
[[FIRAdditionalUserInfo alloc] initWithProviderID:nil
profile:nil
username:nil
isNewUser:NO];
FIRAuthDataResult *authDataResult =
[[FIRAuthDataResult alloc] initWithUser:_currentUser
additionalUserInfo:additionalUserInfo];
decoratedCallback(authDataResult, nil);
return;
}
[self internalSignInAnonymouslyWithCompletion:^(FIRSignUpNewUserResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
decoratedCallback(nil, error);
return;
}
[self completeSignInWithAccessToken:response.IDToken
accessTokenExpirationDate:response.approximateExpirationDate
refreshToken:response.refreshToken
anonymous:YES
callback:^(FIRUser *_Nullable user, NSError *_Nullable error) {
FIRAdditionalUserInfo *additionalUserInfo =
[[FIRAdditionalUserInfo alloc] initWithProviderID:nil
profile:nil
username:nil
isNewUser:YES];
FIRAuthDataResult *authDataResult =
[[FIRAuthDataResult alloc] initWithUser:user
additionalUserInfo:additionalUserInfo];
decoratedCallback(authDataResult, nil);
}];
}];
});
}

- (void)signInAnonymouslyWithCompletion:(FIRAuthResultCallback)completion {
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
FIRAuthResultCallback decoratedCallback =
Expand All @@ -684,11 +725,8 @@ - (void)signInAnonymouslyWithCompletion:(FIRAuthResultCallback)completion {
decoratedCallback(_currentUser, nil);
return;
}
FIRSignUpNewUserRequest *request =
[[FIRSignUpNewUserRequest alloc]initWithRequestConfiguration:_requestConfiguration];
[FIRAuthBackend signUpNewUser:request
callback:^(FIRSignUpNewUserResponse *_Nullable response,
NSError *_Nullable error) {
[self internalSignInAnonymouslyWithCompletion:^(FIRSignUpNewUserResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
decoratedCallback(nil, error);
return;
Expand Down Expand Up @@ -1092,6 +1130,16 @@ - (void)phoneNumberSignInWithRequest:(FIRVerifyPhoneNumberRequest *)request
}
#endif

/** @fn internalSignInAnonymouslyWithCompletion:
@param completion A block which is invoked when the anonymous sign in request completes.
*/
- (void)internalSignInAnonymouslyWithCompletion:(FIRSignupNewUserCallback)completion {
FIRSignUpNewUserRequest *request =
[[FIRSignUpNewUserRequest alloc]initWithRequestConfiguration:_requestConfiguration];
[FIRAuthBackend signUpNewUser:request
callback:completion];
}

/** @fn possiblyPostAuthStateChangeNotification
@brief Posts the auth state change notificaton if current user's token has been changed.
*/
Expand Down
23 changes: 23 additions & 0 deletions Firebase/Auth/Source/Public/FIRAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,29 @@ FIR_SWIFT_NAME(Auth)
*/
- (void)signInAnonymouslyWithCompletion:(nullable FIRAuthResultCallback)completion;

/** @fn signInAnonymouslyAndRetrieveDataWithCompletion:
@brief Asynchronously creates and becomes an anonymous user.
@param completion Optionally; a block which is invoked when the sign in finishes, or is
canceled. Invoked asynchronously on the main thread in the future.

@remarks If there is already an anonymous user signed in, that user will be returned instead.
If there is any other existing user signed in, that user will be signed out.

@remarks Possible error codes:
<ul>
<li>@c FIRAuthErrorCodeOperationNotAllowed - Indicates that anonymous accounts are
not enabled. Enable them in the Auth section of the Firebase console.
</li>
</ul>

@remarks See @c FIRAuthErrors for a list of error codes that are common to all API methods.
@remarks This method will only exist until the next major Firebase release following 4.x.x.
After the next major release the method @c signInAnonymouslyWithCompletion will support the
@c FIRAuthDataResultCallback.
*/
- (void)signInAnonymouslyAndRetrieveDataWithCompletion:
(nullable FIRAuthDataResultCallback)completion;

/** @fn signInWithCustomToken:completion:
@brief Asynchronously signs in to Firebase with the given Auth token.

Expand Down