GoogleSignInAuthenticator.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #if os(iOS)
  19. import UIKit
  20. #elseif os(macOS)
  21. import AppKit
  22. #endif
  23. /// A class for authenticating via Google.
  24. final class GoogleSignInAuthenticator {
  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. #if os(iOS)
  32. /// Signs in the user based upon the selected account.
  33. /// - parameter rootViewController: The `UIViewController` to use during the sign in flow.
  34. /// - returns: The `GIDSignInResult`.
  35. /// - throws: Any error that may arise during the sign in process.
  36. @MainActor
  37. func signIn(with rootViewController: UIViewController) async throws -> GIDSignInResult {
  38. return try await GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController)
  39. }
  40. #endif
  41. #if os(macOS)
  42. /// Signs in the user based upon the selected account.
  43. /// - parameter window: The `NSWindow` to use during the sign in flow.
  44. /// - returns: The `GIDSignInResult`.
  45. /// - throws: Any error that may arise during the sign in process.
  46. @MainActor
  47. func signIn(with window: NSWindow) async throws -> GIDSignInResult {
  48. return try await GIDSignIn.sharedInstance.signIn(withPresenting: window)
  49. }
  50. #endif
  51. /// Signs out the current user.
  52. func signOut() {
  53. GIDSignIn.sharedInstance.signOut()
  54. authViewModel.state = .signedOut
  55. }
  56. /// Disconnects the previously granted scope and signs the user out.
  57. @MainActor
  58. func disconnect() async throws {
  59. try await GIDSignIn.sharedInstance.disconnect()
  60. authViewModel.state = .signedOut
  61. }
  62. #if os(iOS)
  63. /// Adds the birthday read scope for the current user.
  64. /// - parameter viewController: The `UIViewController` to use while authorizing the scope.
  65. /// - returns: The `GIDSignInResult`.
  66. /// - throws: Any error that may arise while authorizing the scope.
  67. @MainActor
  68. func addBirthdayReadScope(viewController: UIViewController) async throws -> GIDSignInResult {
  69. guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
  70. fatalError("No currentUser!")
  71. }
  72. return try await currentUser.addScopes(
  73. [BirthdayLoader.birthdayReadScope],
  74. presenting: viewController
  75. )
  76. }
  77. #endif
  78. #if os(macOS)
  79. /// Adds the birthday read scope for the current user.
  80. /// - parameter window: The `NSWindow` to use while authorizing the scope.
  81. /// - returns: The `GIDSignInResult`.
  82. /// - throws: Any error that may arise while authorizing the scope.
  83. @MainActor
  84. func addBirthdayReadScope(window: NSWindow) async throws -> GIDSignInResult {
  85. guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
  86. fatalError("No currentUser!")
  87. }
  88. return try await currentUser.addScopes(
  89. [BirthdayLoader.birthdayReadScope],
  90. presenting: window
  91. )
  92. }
  93. #endif
  94. }
  95. extension GoogleSignInAuthenticator {
  96. enum Error: Swift.Error {
  97. case failedToSignIn
  98. case failedToAddBirthdayReadScope(Swift.Error)
  99. case userUnexpectedlyNilWhileAddingBirthdayReadScope
  100. }
  101. }