diff --git a/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift b/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift index 05b6da5..22e18dd 100644 --- a/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift +++ b/DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift @@ -18,6 +18,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService { private let messaging = Messaging.messaging() private var user: User? { Auth.auth().currentUser } private let providerID = AuthProviderID.gitHub + private let provider = TopViewControllerProvider() private let logger = Logger(category: "GithubAuthService") func signIn() async throws -> AuthenticationDataResponse { @@ -143,6 +144,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService { } } + @MainActor func requestAuthorizationCode() async throws -> String { guard let clientID = Bundle.main.object(forInfoDictionaryKey: "GITHUB_CLIENT_ID") as? String, let redirectURL = Bundle.main.object(forInfoDictionaryKey: "APP_REDIRECT_URL") as? String, @@ -248,12 +250,7 @@ final class GithubAuthenticationService: NSObject, AuthenticationService { extension GithubAuthenticationService: ASWebAuthenticationPresentationContextProviding { func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { - guard let window = UIApplication.shared.connectedScenes - .flatMap({ ($0 as? UIWindowScene)?.windows ?? [] }) - .first(where: { $0.isKeyWindow }) else { - return ASPresentationAnchor() - } - return window + return provider.keyWindow() ?? ASPresentationAnchor() } struct GitHubUser: Codable { diff --git a/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift b/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift index 5798975..55c4b1f 100644 --- a/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift +++ b/DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift @@ -126,35 +126,3 @@ final class GoogleAuthenticationService: AuthenticationService { } } - -final class TopViewControllerProvider { - @MainActor - func topViewController() -> UIViewController? { - let keyWindow = UIApplication.shared.connectedScenes - .compactMap { $0 as? UIWindowScene } - .flatMap { $0.windows } - .first { $0.isKeyWindow } - - let rootController = keyWindow?.rootViewController - - return topViewController(controller: rootController) - } - - @MainActor - private func topViewController(controller: UIViewController?) -> UIViewController? { - if let navigationController = controller as? UINavigationController { - return topViewController(controller: navigationController.visibleViewController) - } - - if let tabController = controller as? UITabBarController, - let selectedController = tabController.selectedViewController { - return topViewController(controller: selectedController) - } - - if let presentedController = controller?.presentedViewController { - return topViewController(controller: presentedController) - } - - return controller - } -} diff --git a/DevLog/Infra/Util/TopViewControllerProvider.swift b/DevLog/Infra/Util/TopViewControllerProvider.swift new file mode 100644 index 0000000..ba5304c --- /dev/null +++ b/DevLog/Infra/Util/TopViewControllerProvider.swift @@ -0,0 +1,46 @@ +// +// TopViewControllerProvider.swift +// DevLog +// +// Created by 최윤진 on 2/12/26. +// + +import UIKit + +final class TopViewControllerProvider { + @MainActor + func topViewController() -> UIViewController? { + guard let keyWindow = keyWindow() else { + return nil + } + + let rootController = keyWindow.rootViewController + return topViewController(controller: rootController) + } + + @MainActor + func keyWindow() -> UIWindow? { + return UIApplication.shared.connectedScenes + .compactMap { $0 as? UIWindowScene } + .flatMap { $0.windows } + .first { $0.isKeyWindow } + } + + @MainActor + private func topViewController(controller: UIViewController?) -> UIViewController? { + if let navigationController = controller as? UINavigationController { + return topViewController(controller: navigationController.visibleViewController) + } + + if let tabController = controller as? UITabBarController, + let selectedController = tabController.selectedViewController { + return topViewController(controller: selectedController) + } + + if let presentedController = controller?.presentedViewController { + return topViewController(controller: presentedController) + } + + return controller + } +}