GoogleSignInAuthenticator.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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) { userAuth, error in
  35. guard let userAuth = userAuth else {
  36. print("Error! \(String(describing: error))")
  37. return
  38. }
  39. self.authViewModel.state = .signedIn(userAuth.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) { userAuth, error in
  47. guard let userAuth = userAuth else {
  48. print("Error! \(String(describing: error))")
  49. return
  50. }
  51. self.authViewModel.state = .signedIn(userAuth.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. guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
  77. fatalError("No user signed in!")
  78. }
  79. #if os(iOS)
  80. guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
  81. fatalError("No root view controller!")
  82. }
  83. currentUser.addScopes([BirthdayLoader.birthdayReadScope],
  84. presenting: rootViewController) { userAuth, error in
  85. if let error = error {
  86. print("Found error while adding birthday read scope: \(error).")
  87. return
  88. }
  89. guard let userAuth = userAuth else { return }
  90. self.authViewModel.state = .signedIn(userAuth.user)
  91. completion()
  92. }
  93. #elseif os(macOS)
  94. guard let presentingWindow = NSApplication.shared.windows.first else {
  95. fatalError("No presenting window!")
  96. }
  97. currentUser.addScopes([BirthdayLoader.birthdayReadScope],
  98. presenting: presentingWindow) { userAuth, error in
  99. if let error = error {
  100. print("Found error while adding birthday read scope: \(error).")
  101. return
  102. }
  103. guard let userAuth = userAuth else { return }
  104. self.authViewModel.state = .signedIn(userAuth.user)
  105. completion()
  106. }
  107. #endif
  108. }
  109. }