From a75b5ab3ada092cf8b00a1204b2b0c19092c903a Mon Sep 17 00:00:00 2001 From: Peter Andrews Date: Thu, 8 Dec 2022 14:59:25 -0800 Subject: [PATCH] Cleanup. --- .../Shared/Services/BirthdayLoader.swift | 13 ------------ .../Services/GoogleSignInAuthenticator.swift | 16 +++++++------- .../ViewModels/AuthenticationViewModel.swift | 21 ++++++++----------- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift index ac136ca3..59bdee2e 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/BirthdayLoader.swift @@ -38,19 +38,6 @@ final class BirthdayLoader { return URLRequest(url: url) }() - private lazy var session: URLSession? = { - guard let accessToken = GIDSignIn - .sharedInstance - .currentUser? - .accessToken - .tokenString else { return nil } - let configuration = URLSessionConfiguration.default - configuration.httpAdditionalHeaders = [ - "Authorization": "Bearer \(accessToken)" - ] - return URLSession(configuration: configuration) - }() - private func sessionWithFreshToken() async throws -> URLSession { guard let user = GIDSignIn.sharedInstance.currentUser else { throw Error.noCurrentUserForSessionWithFreshToken diff --git a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift index 931cf714..1657ce9a 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/Services/GoogleSignInAuthenticator.swift @@ -33,25 +33,25 @@ final class GoogleSignInAuthenticator { } - #if os(iOS) +#if os(iOS) /// Signs in the user based upon the selected account. /// - parameter rootViewController: The `UIViewController` to use during the sign in flow. - /// - returns: The signed in `GIDGoogleUser`. + /// - returns: The resulting`GIDUserAuth`. /// - throws: Any error that may arise during the sign in process. func signIn(with rootViewController: UIViewController) async throws -> GIDUserAuth { return try await GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController) } - #endif +#endif - #if os(macOS) +#if os(macOS) /// Signs in the user based upon the selected account. /// - parameter window: The `NSWindow` to use during the sign in flow. - /// - returns: The signed in `GIDGoogleUser`. + /// - returns: The resulting`GIDUserAuth`. /// - throws: Any error that may arise during the sign in process. func signIn(with window: NSWindow) async throws -> GIDUserAuth { return try await GIDSignIn.sharedInstance.signIn(withPresenting: window) } - #endif +#endif /// Signs out the current user. func signOut() { @@ -67,7 +67,7 @@ final class GoogleSignInAuthenticator { #if os(iOS) /// Adds the birthday read scope for the current user. /// - parameter viewController: The `UIViewController` to use while authorizing the scope. - /// - returns: The `GIDGoogleUser` with the authorized scope. + /// - returns: The resulting`GIDUserAuth`. /// - throws: Any error that may arise while authorizing the scope. func addBirthdayReadScope(viewController: UIViewController) async throws -> GIDUserAuth { guard let currentUser = GIDSignIn.sharedInstance.currentUser else { @@ -83,7 +83,7 @@ final class GoogleSignInAuthenticator { #if os(macOS) /// Adds the birthday read scope for the current user. /// - parameter window: The `NSWindow` to use while authorizing the scope. - /// - returns: The `GIDGoogleUser` with the authorized scope. + /// - returns: The resulting`GIDUserAuth`. /// - throws: Any error that may arise while authorizing the scope. func addBirthdayReadScope(window: NSWindow) async throws -> GIDUserAuth { guard let currentUser = GIDSignIn.sharedInstance.currentUser else { diff --git a/Samples/Swift/DaysUntilBirthday/Shared/ViewModels/AuthenticationViewModel.swift b/Samples/Swift/DaysUntilBirthday/Shared/ViewModels/AuthenticationViewModel.swift index 78a7c839..fc6962fa 100644 --- a/Samples/Swift/DaysUntilBirthday/Shared/ViewModels/AuthenticationViewModel.swift +++ b/Samples/Swift/DaysUntilBirthday/Shared/ViewModels/AuthenticationViewModel.swift @@ -52,30 +52,26 @@ final class AuthenticationViewModel: ObservableObject { print("There is no root view controller!") return } - - Task { @MainActor in - do { - let userAuth = try await authenticator.signIn(with: rootViewController) - self.state = .signedIn(userAuth.user) - } catch { - print("Error signing in: \(error)") - } - } #elseif os(macOS) guard let presentingWindow = NSApplication.shared.windows.first else { print("There is no presenting window!") return } +#endif Task { @MainActor in do { + +#if os(iOS) + let userAuth = try await authenticator.signIn(with: rootViewController) +#elseif os(macOS) let userAuth = try await authenticator.signIn(with: presentingWindow) +#endif self.state = .signedIn(userAuth.user) } catch { print("Error signing in: \(error)") } } -#endif } /// Signs the user out. @@ -102,17 +98,18 @@ final class AuthenticationViewModel: ObservableObject { #if os(iOS) /// Adds the requested birthday read scope. /// - parameter viewController: A `UIViewController` to use while presenting the flow. - /// - returns: A `GIDGoogleUser` with the authorized scope. + /// - returns: The resulting`GIDUserAuth`. /// - throws: Any error that may arise while adding the read birthday scope. func addBirthdayReadScope(viewController: UIViewController) async throws -> GIDUserAuth { return try await authenticator.addBirthdayReadScope(viewController: viewController) } #endif + #if os(macOS) /// adds the requested birthday read scope. /// - parameter window: An `NSWindow` to use while presenting the flow. - /// - returns: A `GIDGoogleUser` with the authorized scope. + /// - returns: The resulting `GIDUserAuth`. /// - throws: Any error that may arise while adding the read birthday scope. func addBirthdayReadScope(window: NSWindow) async throws -> GIDUserAuth { return try await authenticator.addBirthdayReadScope(window: window)