GoogleSignInAuthenticator.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. private let clientID = "687389107077-8qr6dh8fr4uaja89sdr5ieqb7mep04qv.apps.googleusercontent.com"
  22. private lazy var configuration: GIDConfiguration = {
  23. return GIDConfiguration(clientID: clientID)
  24. }()
  25. private var authViewModel: AuthenticationViewModel
  26. /// Creates an instance of this authenticator.
  27. /// - parameter authViewModel: The view model this authenticator will set logged in status on.
  28. init(authViewModel: AuthenticationViewModel) {
  29. self.authViewModel = authViewModel
  30. }
  31. /// Signs in the user based upon the selected account.'
  32. /// - note: Successful calls to this will set the `authViewModel`'s `state` property.
  33. func signIn() {
  34. guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
  35. print("There is no root view controller!")
  36. return
  37. }
  38. GIDSignIn.sharedInstance.signIn(with: configuration,
  39. presenting: rootViewController) { user, error in
  40. guard let user = user else {
  41. print("Error! \(String(describing: error))")
  42. return
  43. }
  44. self.authViewModel.state = .signedIn(user)
  45. }
  46. }
  47. /// Signs out the current user.
  48. func signOut() {
  49. GIDSignIn.sharedInstance.signOut()
  50. authViewModel.state = .signedOut
  51. }
  52. /// Adds the birthday read scope for the current user.
  53. /// - parameter completion: An escaping closure that is called upon successful completion of the
  54. /// `addScopes(_:presenting:)` request.
  55. /// - note: Successful requests will update the `authViewModel.state` with a new current user that
  56. /// has the granted scope.
  57. func addBirthdayReadScope(completion: @escaping () -> Void) {
  58. guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
  59. fatalError("No root view controller!")
  60. }
  61. GIDSignIn.sharedInstance.addScopes([BirthdayLoader.birthdayReadScope],
  62. presenting: rootViewController) { user, error in
  63. if let error = error {
  64. print("Found error while adding birthday read scope: \(error).")
  65. return
  66. }
  67. guard let currentUser = user else { return }
  68. self.authViewModel.state = .signedIn(currentUser)
  69. completion()
  70. }
  71. }
  72. /// Disconnects the previously granted scope and signs the user out.
  73. func disconnect() {
  74. GIDSignIn.sharedInstance.disconnect { error in
  75. if let error = error {
  76. print("Encountered error disconnecting scope: \(error).")
  77. }
  78. self.signOut()
  79. }
  80. }
  81. }