GoogleSignInAuthenticator.swift 4.5 KB

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