Peter Andrews 3 år sedan
förälder
incheckning
a75b5ab3ad

+ 0 - 13
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

+ 8 - 8
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 {

+ 9 - 12
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)