GoogleAuthProvider.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "GoogleAuthProvider.h"
  17. #import <GoogleSignIn/GoogleSignIn.h>
  18. #import "AppManager.h"
  19. #import <FirebaseCore/FIRApp.h>
  20. #import <FirebaseCore/FIROptions.h>
  21. #import <FirebaseAuth/FIRGoogleAuthProvider.h>
  22. @implementation GoogleAuthProvider
  23. - (void)getAuthCredentialWithPresentingViewController:(UIViewController *)viewController
  24. callback:(AuthCredentialCallback)callback {
  25. [self signOut];
  26. GIDSignIn *signIn = GIDSignIn.sharedInstance;
  27. GIDConfiguration *config = [[GIDConfiguration alloc] initWithClientID:[self googleClientID]];
  28. [signIn signInWithConfiguration:config
  29. presentingViewController:viewController
  30. callback:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {
  31. if (error) {
  32. callback(nil, error);
  33. return;
  34. }
  35. GIDAuthentication *auth = user.authentication;
  36. FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:auth.idToken
  37. accessToken:auth.accessToken];
  38. callback(credential, error);
  39. }];
  40. }
  41. - (void)signOut {
  42. [GIDSignIn.sharedInstance signOut];
  43. }
  44. - (NSString *)googleClientID {
  45. return [AppManager app].options.clientID;
  46. }
  47. @end