FacebookAuthProvider.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FacebookAuthProvider.h"
  17. #import <FBSDKCoreKit/FBSDKCoreKit.h>
  18. #import <FBSDKLoginKit/FBSDKLoginKit.h>
  19. #import "FIRFacebookAuthProvider.h"
  20. #import "ApplicationDelegate.h"
  21. #import "AuthCredentials.h"
  22. /** @var kFacebookAppId
  23. @brief The App ID for the Facebook SDK.
  24. */
  25. static NSString *const kFacebookAppID = KFACEBOOK_APP_ID;
  26. @interface FacebookAuthProvider () <OpenURLDelegate>
  27. @end
  28. @implementation FacebookAuthProvider {
  29. FBSDKLoginManager *_loginManager;
  30. }
  31. - (instancetype)init {
  32. self = [super init];
  33. if (self) {
  34. _loginManager = [[FBSDKLoginManager alloc] init];
  35. }
  36. return self;
  37. }
  38. - (void)getAuthCredentialWithPresentingViewController:(UIViewController *)viewController
  39. callback:(AuthCredentialCallback)callback {
  40. [self signOut];
  41. [ApplicationDelegate setOpenURLDelegate:self];
  42. [FBSDKSettings setAppID:kFacebookAppID];
  43. [_loginManager logInWithPermissions:@[ @"email" ]
  44. fromViewController:viewController
  45. handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
  46. [ApplicationDelegate setOpenURLDelegate:nil];
  47. if (!error && result.isCancelled) {
  48. error = [NSError errorWithDomain:@"com.google.FirebaseAuthSample" code:-1 userInfo:nil];
  49. }
  50. if (error) {
  51. callback(nil, error);
  52. return;
  53. }
  54. NSString *accessToken = [FBSDKAccessToken currentAccessToken].tokenString;
  55. callback([FIRFacebookAuthProvider credentialWithAccessToken:accessToken], nil);
  56. }];
  57. }
  58. - (void)signOut {
  59. [_loginManager logOut];
  60. }
  61. - (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(NSString *)sourceApplication {
  62. return [[FBSDKApplicationDelegate sharedInstance] application:[UIApplication sharedApplication]
  63. openURL:URL
  64. sourceApplication:sourceApplication
  65. annotation:nil];
  66. }
  67. @end