1616
1717import Foundation
1818import GoogleSignIn
19+ #if os(iOS)
20+ import UIKit
21+ #elseif os(macOS)
22+ import AppKit
23+ #endif
1924
2025/// An observable class for authenticating via Google.
21- final class GoogleSignInAuthenticator : ObservableObject {
26+ final class GoogleSignInAuthenticator {
2227 // TODO: Replace this with your own ID.
2328 #if os(iOS)
2429 private let clientID = " 687389107077-8qr6dh8fr4uaja89sdr5ieqb7mep04qv.apps.googleusercontent.com "
@@ -38,40 +43,32 @@ final class GoogleSignInAuthenticator: ObservableObject {
3843 self . authViewModel = authViewModel
3944 }
4045
41- /// Signs in the user based upon the selected account.'
42- /// - note: Successful calls to this will set the `authViewModel`'s `state` property.
43- func signIn( ) {
44- #if os(iOS)
45- guard let rootViewController = UIApplication . shared. windows. first? . rootViewController else {
46- print ( " There is no root view controller! " )
47- return
48- }
49-
50- GIDSignIn . sharedInstance. signIn ( with: configuration,
51- presenting: rootViewController) { user, error in
52- guard let user = user else {
53- print ( " Error! \( String ( describing: error) ) " )
54- return
55- }
56- self . authViewModel. state = . signedIn( user)
57- }
5846
59- #elseif os(macOS)
60- guard let presentingWindow = NSApplication . shared. windows. first else {
61- print ( " There is no presenting window! " )
62- return
63- }
47+ #if os(iOS)
48+ /// Signs in the user based upon the selected account.
49+ /// - parameter rootViewController: The `UIViewController` to use during the sign in flow.
50+ /// - returns: The signed in `GIDGoogleUser`.
51+ /// - throws: Any error that may arise during the sign in process.
52+ func signIn( with rootViewController: UIViewController ) async throws -> GIDGoogleUser {
53+ return try await GIDSignIn . sharedInstance. signIn (
54+ with: configuration,
55+ presenting: rootViewController
56+ )
57+ }
58+ #endif
6459
65- GIDSignIn . sharedInstance. signIn ( with: configuration,
66- presenting: presentingWindow) { user, error in
67- guard let user = user else {
68- print ( " Error! \( String ( describing: error) ) " )
69- return
70- }
71- self . authViewModel. state = . signedIn( user)
72- }
73- #endif
60+ #if os(macOS)
61+ /// Signs in the user based upon the selected account.
62+ /// - parameter window: The `NSWindow` to use during the sign in flow.
63+ /// - returns: The signed in `GIDGoogleUser`.
64+ /// - throws: Any error that may arise during the sign in process.
65+ func signIn( with window: NSWindow ) async throws -> GIDGoogleUser {
66+ return try await GIDSignIn . sharedInstance. signIn (
67+ with: configuration,
68+ presenting: window
69+ )
7470 }
71+ #endif
7572
7673 /// Signs out the current user.
7774 func signOut( ) {
@@ -80,57 +77,41 @@ final class GoogleSignInAuthenticator: ObservableObject {
8077 }
8178
8279 /// Disconnects the previously granted scope and signs the user out.
83- func disconnect( ) {
84- GIDSignIn . sharedInstance. disconnect { error in
85- if let error = error {
86- print ( " Encountered error disconnecting scope: \( error) . " )
87- }
88- self . signOut ( )
89- }
80+ func disconnect( ) async throws {
81+ try await GIDSignIn . sharedInstance. disconnect ( )
9082 }
9183
92- // Confines birthday calucation to iOS for now.
84+ #if os( iOS)
9385 /// Adds the birthday read scope for the current user.
94- /// - parameter completion: An escaping closure that is called upon successful completion of the
95- /// `addScopes(_:presenting:)` request.
96- /// - note: Successful requests will update the `authViewModel.state` with a new current user that
97- /// has the granted scope.
98- func addBirthdayReadScope( completion: @escaping ( ) -> Void ) {
99- #if os(iOS)
100- guard let rootViewController = UIApplication . shared. windows. first? . rootViewController else {
101- fatalError ( " No root view controller! " )
102- }
103-
104- GIDSignIn . sharedInstance. addScopes ( [ BirthdayLoader . birthdayReadScope] ,
105- presenting: rootViewController) { user, error in
106- if let error = error {
107- print ( " Found error while adding birthday read scope: \( error) . " )
108- return
109- }
110-
111- guard let currentUser = user else { return }
112- self . authViewModel. state = . signedIn( currentUser)
113- completion ( )
114- }
115-
116- #elseif os(macOS)
117- guard let presentingWindow = NSApplication . shared. windows. first else {
118- fatalError ( " No presenting window! " )
119- }
120-
121- GIDSignIn . sharedInstance. addScopes ( [ BirthdayLoader . birthdayReadScope] ,
122- presenting: presentingWindow) { user, error in
123- if let error = error {
124- print ( " Found error while adding birthday read scope: \( error) . " )
125- return
126- }
127-
128- guard let currentUser = user else { return }
129- self . authViewModel. state = . signedIn( currentUser)
130- completion ( )
131- }
86+ /// - parameter viewController: The `UIViewController` to use while authorizing the scope.
87+ /// - returns: The `GIDGoogleUser` with the authorized scope.
88+ /// - throws: Any error that may arise while authorizing the scope.
89+ func addBirthdayReadScope( viewController: UIViewController ) async throws -> GIDGoogleUser {
90+ return try await GIDSignIn . sharedInstance. addScopes (
91+ [ BirthdayLoader . birthdayReadScope] ,
92+ presenting: viewController
93+ )
94+ }
95+ #endif
13296
133- #endif
97+ #if os(macOS)
98+ /// Adds the birthday read scope for the current user.
99+ /// - parameter window: The `NSWindow` to use while authorizing the scope.
100+ /// - returns: The `GIDGoogleUser` with the authorized scope.
101+ /// - throws: Any error that may arise while authorizing the scope.
102+ func addBirthdayReadScope( window: NSWindow ) async throws -> GIDGoogleUser {
103+ return try await GIDSignIn . sharedInstance. addScopes (
104+ [ BirthdayLoader . birthdayReadScope] ,
105+ presenting: window
106+ )
134107 }
108+ #endif
109+ }
135110
111+ extension GoogleSignInAuthenticator {
112+ enum Error : Swift . Error {
113+ case failedToSignIn
114+ case failedToAddBirthdayReadScope( Swift . Error )
115+ case userUnexpectedlyNilWhileAddingBirthdayReadScope
116+ }
136117}
0 commit comments