diff options
author | Zsika Phillip <protocol86@users.noreply.github.com> | 2017-11-17 15:55:56 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-17 15:55:56 -0800 |
commit | 5fbe71052de048a15f44476115a49e1958e70691 (patch) | |
tree | fead2d827d2264005dc82dd728b63a84f25dc156 /Firebase | |
parent | 4fbc96904fe86b3a0cdb91bfa6d96491221eb6b9 (diff) |
Adds AuthDataResult to anonymous sign in (#470)
* Adds AuthDataResult to anonymous sign in
* Fixes typo
* Addresses comments
* addresses comment on PR
Diffstat (limited to 'Firebase')
-rw-r--r-- | Firebase/Auth/Source/FIRAdditionalUserInfo.m | 2 | ||||
-rw-r--r-- | Firebase/Auth/Source/FIRAdditionalUserInfo_Internal.h | 2 | ||||
-rw-r--r-- | Firebase/Auth/Source/FIRAuth.m | 58 | ||||
-rw-r--r-- | Firebase/Auth/Source/Public/FIRAuth.h | 23 |
4 files changed, 78 insertions, 7 deletions
diff --git a/Firebase/Auth/Source/FIRAdditionalUserInfo.m b/Firebase/Auth/Source/FIRAdditionalUserInfo.m index 3fee504..c6ba37c 100644 --- a/Firebase/Auth/Source/FIRAdditionalUserInfo.m +++ b/Firebase/Auth/Source/FIRAdditionalUserInfo.m @@ -50,7 +50,7 @@ static NSString *const kNewUserKey = @"newUser"; 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 { diff --git a/Firebase/Auth/Source/FIRAdditionalUserInfo_Internal.h b/Firebase/Auth/Source/FIRAdditionalUserInfo_Internal.h index a813566..c8e345d 100644 --- a/Firebase/Auth/Source/FIRAdditionalUserInfo_Internal.h +++ b/Firebase/Auth/Source/FIRAdditionalUserInfo_Internal.h @@ -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; diff --git a/Firebase/Auth/Source/FIRAuth.m b/Firebase/Auth/Source/FIRAuth.m index 6c349a1..fda84fa 100644 --- a/Firebase/Auth/Source/FIRAuth.m +++ b/Firebase/Auth/Source/FIRAuth.m @@ -676,6 +676,47 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; }]; } +- (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 = @@ -684,11 +725,8 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; 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; @@ -1092,6 +1130,16 @@ static NSMutableDictionary *gKeychainServiceNameForAppName; } #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. */ diff --git a/Firebase/Auth/Source/Public/FIRAuth.h b/Firebase/Auth/Source/Public/FIRAuth.h index eabcf23..2963d7b 100644 --- a/Firebase/Auth/Source/Public/FIRAuth.h +++ b/Firebase/Auth/Source/Public/FIRAuth.h @@ -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. |