GoogleSignInAuthenticator.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2021 Google LLC
  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 Foundation
  17. import GoogleSignIn
  18. /// An observable class for authenticating via Google.
  19. final class GoogleSignInAuthenticator: ObservableObject {
  20. private var authViewModel: AuthenticationViewModel
  21. /// Creates an instance of this authenticator.
  22. /// - parameter authViewModel: The view model this authenticator will set logged in status on.
  23. init(authViewModel: AuthenticationViewModel) {
  24. self.authViewModel = authViewModel
  25. }
  26. /// Signs in the user based upon the selected account.'
  27. /// - note: Successful calls to this will set the `authViewModel`'s `state` property.
  28. func signIn() {
  29. #if os(iOS)
  30. guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
  31. print("There is no root view controller!")
  32. return
  33. }
  34. GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController) { user, error in
  35. guard let user = user else {
  36. print("Error! \(String(describing: error))")
  37. return
  38. }
  39. self.authViewModel.state = .signedIn(user)
  40. }
  41. #elseif os(macOS)
  42. guard let presentingWindow = NSApplication.shared.windows.first else {
  43. print("There is no presenting window!")
  44. return
  45. }
  46. GIDSignIn.sharedInstance.signIn(withPresenting: presentingWindow) { user, error in
  47. guard let user = user else {
  48. print("Error! \(String(describing: error))")
  49. return
  50. }
  51. self.authViewModel.state = .signedIn(user)
  52. }
  53. #endif
  54. }
  55. /// Signs out the current user.
  56. func signOut() {
  57. GIDSignIn.sharedInstance.signOut()
  58. authViewModel.state = .signedOut
  59. }
  60. /// Disconnects the previously granted scope and signs the user out.
  61. func disconnect() {
  62. GIDSignIn.sharedInstance.disconnect { error in
  63. if let error = error {
  64. print("Encountered error disconnecting scope: \(error).")
  65. }
  66. self.signOut()
  67. }
  68. }
  69. // Confines birthday calucation to iOS for now.
  70. /// Adds the birthday read scope for the current user.
  71. /// - parameter completion: An escaping closure that is called upon successful completion of the
  72. /// `addScopes(_:presenting:)` request.
  73. /// - note: Successful requests will update the `authViewModel.state` with a new current user that
  74. /// has the granted scope.
  75. func addBirthdayReadScope(completion: @escaping () -> Void) {
  76. #if os(iOS)
  77. guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
  78. fatalError("No root view controller!")
  79. }
  80. GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope],
  81. presenting: rootViewController) { user, error in
  82. if let error = error {
  83. print("Found error while adding birthday read scope: \(error).")
  84. return
  85. }
  86. guard let currentUser = user else { return }
  87. self.authViewModel.state = .signedIn(currentUser)
  88. completion()
  89. }
  90. #elseif os(macOS)
  91. guard let presentingWindow = NSApplication.shared.windows.first else {
  92. fatalError("No presenting window!")
  93. }
  94. GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope],
  95. presenting: presentingWindow) { user, error in
  96. if let error = error {
  97. print("Found error while adding birthday read scope: \(error).")
  98. return
  99. }
  100. guard let currentUser = user else { return }
  101. self.authViewModel.state = .signedIn(currentUser)
  102. completion()
  103. }
  104. #endif
  105. }
  106. }