Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
petea committed Dec 8, 2022
1 parent 0f00694 commit a75b5ab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit a75b5ab

Please sign in to comment.