GoogleSignInAuthenticator.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// An observable class for authenticating via Google.
  24. final class GoogleSignInAuthenticator: ObservableObject {
  25. // TODO: Replace this with your own ID.
  26. #if os(iOS)
  27. private let clientID = "687389107077-8qr6dh8fr4uaja89sdr5ieqb7mep04qv.apps.googleusercontent.com"
  28. #elseif os(macOS)
  29. private let clientID = "687389107077-8qr6dh8fr4uaja89sdr5ieqb7mep04qv.apps.googleusercontent.com"
  30. #endif
  31. private lazy var configuration: GIDConfiguration = {
  32. return GIDConfiguration(clientID: clientID)
  33. }()
  34. private var authViewModel: AuthenticationViewModel
  35. /// Creates an instance of this authenticator.
  36. /// - parameter authViewModel: The view model this authenticator will set logged in status on.
  37. init(authViewModel: AuthenticationViewModel) {
  38. self.authViewModel = authViewModel
  39. }
  40. #if os(iOS)
  41. /// Signs in the user based upon the selected account.
  42. /// - parameter rootViewController: The `UIViewController` to use during the sign in flow.
  43. /// - returns: The signed in `GIDGoogleUser`.
  44. /// - throws: Any error that may arise during the sign in process.
  45. func signIn(with rootViewController: UIViewController) async throws -> GIDGoogleUser {
  46. return try await GIDSignIn.sharedInstance.signIn(
  47. with: configuration,
  48. presenting: rootViewController
  49. )
  50. }
  51. #endif
  52. #if os(macOS)
  53. /// Signs in the user based upon the selected account.
  54. /// - parameter window: The `NSWindow` to use during the sign in flow.
  55. /// - returns: The signed in `GIDGoogleUser`.
  56. /// - throws: Any error that may arise during the sign in process.
  57. func signIn(with window: NSWindow) async throws -> GIDGoogleUser {
  58. return try await GIDSignIn.sharedInstance.signIn(
  59. with: configuration,
  60. presenting: window
  61. )
  62. }
  63. #endif
  64. /// Signs out the current user.
  65. func signOut() {
  66. GIDSignIn.sharedInstance.signOut()
  67. authViewModel.state = .signedOut
  68. }
  69. /// Disconnects the previously granted scope and signs the user out.
  70. func disconnect() async throws {
  71. try await GIDSignIn.sharedInstance.disconnect()
  72. }
  73. #if os(iOS)
  74. /// Adds the birthday read scope for the current user.
  75. /// - parameter viewController: The `UIViewController` to use while authorizing the scope.
  76. /// - returns: The `GIDGoogleUser` with the authorized scope.
  77. /// - throws: Any error that may arise while authorizing the scope.
  78. func addBirthdayReadScope(viewController: UIViewController) async throws -> GIDGoogleUser {
  79. return try await GIDSignIn.sharedInstance.addScopes(
  80. [BirthdayLoader.birthdayReadScope],
  81. presenting: viewController
  82. )
  83. }
  84. #endif
  85. #if os(macOS)
  86. /// Adds the birthday read scope for the current user.
  87. /// - parameter window: The `NSWindow` to use while authorizing the scope.
  88. /// - returns: The `GIDGoogleUser` with the authorized scope.
  89. /// - throws: Any error that may arise while authorizing the scope.
  90. func addBirthdayReadScope(window: NSWindow) async throws -> GIDGoogleUser {
  91. return try await GIDSignIn.sharedInstance.addScopes(
  92. [BirthdayLoader.birthdayReadScope],
  93. presenting: window
  94. )
  95. }
  96. #endif
  97. }
  98. extension GoogleSignInAuthenticator {
  99. enum Error: Swift.Error {
  100. case failedToSignIn
  101. case failedToAddBirthdayReadScope(Swift.Error)
  102. case userUnexpectedlyNilWhileAddingBirthdayReadScope
  103. }
  104. }