Use env webAuthenticationSession

This commit is contained in:
Thomas Ricouard 2023-12-07 09:45:34 +01:00
parent 5941276145
commit 51656794fc
2 changed files with 7 additions and 33 deletions

View file

@ -1,5 +1,4 @@
import AppAccount import AppAccount
import AuthenticationServices
import Combine import Combine
import DesignSystem import DesignSystem
import Env import Env
@ -15,6 +14,7 @@ struct AddAccountView: View {
@Environment(\.dismiss) private var dismiss @Environment(\.dismiss) private var dismiss
@Environment(\.scenePhase) private var scenePhase @Environment(\.scenePhase) private var scenePhase
@Environment(\.openURL) private var openURL @Environment(\.openURL) private var openURL
@Environment(\.webAuthenticationSession) private var webAuthenticationSession
@Environment(AppAccountsManager.self) private var appAccountsManager @Environment(AppAccountsManager.self) private var appAccountsManager
@Environment(CurrentAccount.self) private var currentAccount @Environment(CurrentAccount.self) private var currentAccount
@ -215,26 +215,12 @@ struct AddAccountView: View {
} }
private func signIn() async { private func signIn() async {
do { signInClient = .init(server: sanitizedName)
signInClient = .init(server: sanitizedName) if let oauthURL = try? await signInClient?.oauthURL(),
if let oauthURL = try await signInClient?.oauthURL() { let url = try? await webAuthenticationSession.authenticate(using: oauthURL,
let session = ASWebAuthenticationSession(url: oauthURL, callbackURLScheme: AppInfo.scheme.replacingOccurrences(of: "://", with: "")){
callbackURLScheme: AppInfo.scheme.replacingOccurrences(of: "://", with: "")) await continueSignIn(url: url)
{ callbackURL, error in } else {
if let callbackURL {
Task {
await continueSignIn(url: callbackURL)
}
} else {
isSigninIn = false
}
}
session.presentationContextProvider = SceneDelegate.authViewController
session.start()
} else {
isSigninIn = false
}
} catch {
isSigninIn = false isSigninIn = false
} }
} }

View file

@ -1,5 +1,4 @@
import Combine import Combine
import AuthenticationServices
import UIKit import UIKit
@Observable @Observable
@ -8,9 +7,6 @@ public class SceneDelegate: NSObject, UIWindowSceneDelegate, Sendable {
public private(set) var windowWidth: CGFloat = UIScreen.main.bounds.size.width public private(set) var windowWidth: CGFloat = UIScreen.main.bounds.size.width
public private(set) var windowHeight: CGFloat = UIScreen.main.bounds.size.height public private(set) var windowHeight: CGFloat = UIScreen.main.bounds.size.height
public static var globalPresentationAnchor: ASPresentationAnchor? = nil
public static var authViewController = AuthViewController()
public func scene(_ scene: UIScene, public func scene(_ scene: UIScene,
willConnectTo _: UISceneSession, willConnectTo _: UISceneSession,
options _: UIScene.ConnectionOptions) options _: UIScene.ConnectionOptions)
@ -18,8 +14,6 @@ public class SceneDelegate: NSObject, UIWindowSceneDelegate, Sendable {
guard let windowScene = scene as? UIWindowScene else { return } guard let windowScene = scene as? UIWindowScene else { return }
window = windowScene.keyWindow window = windowScene.keyWindow
Self.globalPresentationAnchor = window
#if targetEnvironment(macCatalyst) #if targetEnvironment(macCatalyst)
if let titlebar = windowScene.titlebar { if let titlebar = windowScene.titlebar {
titlebar.titleVisibility = .hidden titlebar.titleVisibility = .hidden
@ -60,9 +54,3 @@ public class SceneDelegate: NSObject, UIWindowSceneDelegate, Sendable {
} }
} }
} }
public class AuthViewController: UIViewController, ASWebAuthenticationPresentationContextProviding {
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return SceneDelegate.globalPresentationAnchor ?? ASPresentationAnchor()
}
}