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

View file

@ -1,5 +1,4 @@
import Combine
import AuthenticationServices
import UIKit
@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 windowHeight: CGFloat = UIScreen.main.bounds.size.height
public static var globalPresentationAnchor: ASPresentationAnchor? = nil
public static var authViewController = AuthViewController()
public func scene(_ scene: UIScene,
willConnectTo _: UISceneSession,
options _: UIScene.ConnectionOptions)
@ -18,8 +14,6 @@ public class SceneDelegate: NSObject, UIWindowSceneDelegate, Sendable {
guard let windowScene = scene as? UIWindowScene else { return }
window = windowScene.keyWindow
Self.globalPresentationAnchor = window
#if targetEnvironment(macCatalyst)
if let titlebar = windowScene.titlebar {
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()
}
}