diff --git a/FirebaseFacebookAuthUI/Sources/FUIFacebookAuth.m b/FirebaseFacebookAuthUI/Sources/FUIFacebookAuth.m index d34548007ad..93a3db74301 100644 --- a/FirebaseFacebookAuthUI/Sources/FUIFacebookAuth.m +++ b/FirebaseFacebookAuthUI/Sources/FUIFacebookAuth.m @@ -68,6 +68,11 @@ @interface FUIFacebookAuth () */ @property(nonatomic, strong) FIROAuthProvider *providerForEmulator; +/** @property currentNonce + @brief The nonce for the current Facebook Limited Login session, if any. + */ +@property(nonatomic, copy, nullable) NSString *currentNonce; + @end @implementation FUIFacebookAuth { @@ -131,6 +136,9 @@ - (nullable NSString *)providerID { return FIRFacebookAuthProviderID; } +/** @fn accessToken: + @brief The access token provided by Facebook's login flow. + */ - (nullable NSString *)accessToken { if (self.authUI.isEmulatorEnabled) { return nil; @@ -139,10 +147,13 @@ - (nullable NSString *)accessToken { } /** @fn idToken: - @brief Facebook doesn't provide User Id Token during sign in flow + @brief The ID token provided by Facebook's login flow. */ - (nullable NSString *)idToken { - return nil; + if (self.authUI.isEmulatorEnabled) { + return nil; + } + return FBSDKAuthenticationToken.currentAuthenticationToken.tokenString; } - (NSString *)shortName { @@ -191,29 +202,45 @@ - (void)signInWithDefaultValue:(nullable NSString *)defaultValue return; } - [_loginManager logInWithPermissions:_scopes - fromViewController:presentingViewController - handler:^(FBSDKLoginManagerLoginResult *result, - NSError *error) { - if (error) { - NSError *newError = - [FUIAuthErrorUtils providerErrorWithUnderlyingError:error - providerID:FIRFacebookAuthProviderID]; - [self completeSignInFlowWithAccessToken:nil error:newError]; - } else if (result.isCancelled) { - NSError *newError = [FUIAuthErrorUtils userCancelledSignInError]; - [self completeSignInFlowWithAccessToken:nil error:newError]; - } else { + if (self.useLimitedLogin) { + // Facebook Limited Login + NSString *nonce = [FUIAuthUtils randomNonce]; + self.currentNonce = nonce; + FBSDKLoginConfiguration *configuration = + [[FBSDKLoginConfiguration alloc] initWithPermissions:_scopes + tracking:FBSDKLoginTrackingLimited + nonce:[FUIAuthUtils stringBySHA256HashingString:nonce]]; + [_loginManager logInFromViewController:presentingViewController + configuration:configuration + completion:^(FBSDKLoginManagerLoginResult *result, NSError *error) { + if ([self maybeHandleCancelledResult:result error:error]) { + return; + } + self->_email = FBSDKProfile.currentProfile.email; + NSString *idToken = FBSDKAuthenticationToken.currentAuthenticationToken.tokenString; + [self completeSignInFlowWithAccessToken:nil idToken:idToken error:nil]; + }]; + } else { + [_loginManager logInWithPermissions:_scopes + fromViewController:presentingViewController + handler:^(FBSDKLoginManagerLoginResult *result, + NSError *error) { + if ([self maybeHandleCancelledResult:result error:error]) { + return; + } // Retrieve email. - [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"email" }] startWithCompletion:^(id connection, - id result, - NSError *error) { + [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" + parameters:@{ @"fields" : @"email" }] + startWithCompletion:^(id connection, + id result, + NSError *error) { self->_email = result[@"email"]; }]; [self completeSignInFlowWithAccessToken:result.token.tokenString + idToken:nil error:nil]; - } - }]; + }]; + } } - (void)signInWithOAuthProvider:(FIROAuthProvider *)oauthProvider @@ -269,21 +296,31 @@ - (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(NSString *)sourceApplicati #pragma mark - -/** @fn completeSignInFlowWithAccessToken:error: +/** @fn completeSignInFlowWithAccessToken:idToken:error: @brief Called with the result of a Facebook sign-in attempt. Invokes and clears any pending sign in callback block. - @param accessToken The Facebook access token, if successful. + @param accessToken The Facebook access token, if the Facebook sign-in attempt with tracking enabled is successful. + @param idToken The Facebook ID token, if the Facebook Limited Login attempt is successful. @param error An error which occurred during the sign-in attempt. */ - (void)completeSignInFlowWithAccessToken:(nullable NSString *)accessToken + idToken:(nullable NSString *)idToken error:(nullable NSError *)error { if (error) { [self callbackWithCredential:nil error:error result:nil]; return; } - // Assume accessToken cannot be nil if there's no error. - NSString *_Nonnull token = (id _Nonnull)accessToken; - FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:token]; + FIRAuthCredential *credential; + if (idToken) { + NSString *rawNonce = self.currentNonce; + credential = [FIROAuthProvider credentialWithProviderID:FIRFacebookAuthProviderID + IDToken:idToken + rawNonce:rawNonce]; + } else { + // Assume accessToken cannot be nil if there's no error and idToken is nil. + NSString *_Nonnull token = (id _Nonnull)accessToken; + credential = [FIRFacebookAuthProvider credentialWithAccessToken:token]; + } UIActivityIndicatorView *activityView = [FUIAuthBaseViewController addActivityIndicator:_presentingViewController.view]; [activityView startAnimating]; @@ -347,4 +384,22 @@ - (FBSDKLoginManager *)createLoginManager { return [[FBSDKLoginManager alloc] init]; } +- (BOOL)maybeHandleCancelledResult:(FBSDKLoginManagerLoginResult *)result + error:(NSError *)error { + if (error) { + NSError *newError = + [FUIAuthErrorUtils providerErrorWithUnderlyingError:error + providerID:FIRFacebookAuthProviderID]; + [self completeSignInFlowWithAccessToken:nil idToken:nil error:newError]; + return true; + } + + if (result.isCancelled) { + NSError *newError = [FUIAuthErrorUtils userCancelledSignInError]; + [self completeSignInFlowWithAccessToken:nil idToken:nil error:newError]; + return true; + } + return false; +} + @end diff --git a/FirebaseFacebookAuthUI/Sources/Public/FirebaseFacebookAuthUI/FUIFacebookAuth.h b/FirebaseFacebookAuthUI/Sources/Public/FirebaseFacebookAuthUI/FUIFacebookAuth.h index a425f13b970..ae088d38f8f 100644 --- a/FirebaseFacebookAuthUI/Sources/Public/FirebaseFacebookAuthUI/FUIFacebookAuth.h +++ b/FirebaseFacebookAuthUI/Sources/Public/FirebaseFacebookAuthUI/FUIFacebookAuth.h @@ -48,6 +48,11 @@ NS_ASSUME_NONNULL_BEGIN */ @property(nonatomic, readwrite) FUIButtonAlignment buttonAlignment; +/** @property useLimitedLogin + @brief Whether or not Facebook Login should use Limited Login mode. + */ +@property(nonatomic, assign) BOOL useLimitedLogin; + /** @fn initWithAuthUI @brief Convenience initializer. Uses a default permission of `@[ "email" ]`. @param authUI The @c FUIAuth instance that manages this provider. diff --git a/FirebaseOAuthUI/Sources/FUIOAuth.m b/FirebaseOAuthUI/Sources/FUIOAuth.m index c349ff49f7d..056fdaa8b96 100644 --- a/FirebaseOAuthUI/Sources/FUIOAuth.m +++ b/FirebaseOAuthUI/Sources/FUIOAuth.m @@ -99,6 +99,9 @@ @interface FUIOAuth () @implementation FUIAppDelegate @@ -26,6 +27,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; [GTMSessionFetcher setLoggingEnabled:YES]; + [[FBSDKApplicationDelegate sharedInstance] application:application + didFinishLaunchingWithOptions:launchOptions]; return YES; } @@ -33,6 +36,9 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options { NSString *sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey]; + [[FBSDKApplicationDelegate sharedInstance] application:app + openURL:url + options:options]; return [self handleOpenUrl:url sourceApplication:sourceApplication]; } diff --git a/samples/objc/FirebaseUI-demo-objc/Resources/Main.storyboard b/samples/objc/FirebaseUI-demo-objc/Resources/Main.storyboard index 2502ad19938..7e3485f5983 100644 --- a/samples/objc/FirebaseUI-demo-objc/Resources/Main.storyboard +++ b/samples/objc/FirebaseUI-demo-objc/Resources/Main.storyboard @@ -1,9 +1,9 @@ - + - + @@ -162,7 +162,7 @@ @@ -191,7 +191,7 @@ @@ -225,14 +225,14 @@ - + + + + + @@ -537,6 +548,7 @@ + @@ -561,7 +573,7 @@ - @@ -197,7 +198,7 @@ - + @@ -313,6 +314,17 @@ + + + + + @@ -508,6 +520,7 @@ + @@ -534,7 +547,7 @@ -