|
|
@@ -37,6 +37,7 @@ final class RedditOAuthPanelController: NSObject {
|
|
|
|
|
|
let webView = WKWebView(frame: panel.contentView?.bounds ?? .zero, configuration: RedditWebViewManager.makeConfiguration())
|
|
|
webView.navigationDelegate = self
|
|
|
+ webView.uiDelegate = self
|
|
|
webView.autoresizingMask = [.width, .height]
|
|
|
panel.contentView?.addSubview(webView)
|
|
|
|
|
|
@@ -62,6 +63,22 @@ final class RedditOAuthPanelController: NSObject {
|
|
|
dismiss()
|
|
|
completion?()
|
|
|
}
|
|
|
+
|
|
|
+ private func beginOAuthSignIn(url: URL, in webView: WKWebView) {
|
|
|
+ OAuthAuthenticationManager.shared.authenticate(
|
|
|
+ url: url,
|
|
|
+ presentationWindow: panel
|
|
|
+ ) { [weak self] result in
|
|
|
+ guard let self else { return }
|
|
|
+ switch result {
|
|
|
+ case .success(let callbackURL):
|
|
|
+ webView.load(URLRequest(url: callbackURL))
|
|
|
+ case .failure(let error):
|
|
|
+ self.onAuthError?(error.localizedDescription)
|
|
|
+ self.finish()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
extension RedditOAuthPanelController: WKNavigationDelegate {
|
|
|
@@ -85,19 +102,7 @@ extension RedditOAuthPanelController: WKNavigationDelegate {
|
|
|
|
|
|
if RedditWebAuthHelper.isOAuthProviderURL(url) {
|
|
|
decisionHandler(.cancel)
|
|
|
- OAuthAuthenticationManager.shared.authenticate(
|
|
|
- url: url,
|
|
|
- presentationWindow: panel
|
|
|
- ) { [weak self] result in
|
|
|
- guard let self else { return }
|
|
|
- switch result {
|
|
|
- case .success(let callbackURL):
|
|
|
- webView.load(URLRequest(url: callbackURL))
|
|
|
- case .failure(let error):
|
|
|
- self.onAuthError?(error.localizedDescription)
|
|
|
- self.finish()
|
|
|
- }
|
|
|
- }
|
|
|
+ beginOAuthSignIn(url: url, in: webView)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -123,3 +128,22 @@ extension RedditOAuthPanelController: WKNavigationDelegate {
|
|
|
return RedditWebAuthHelper.looksLikeLoggedInURL(url)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+extension RedditOAuthPanelController: WKUIDelegate {
|
|
|
+ func webView(
|
|
|
+ _ webView: WKWebView,
|
|
|
+ createWebViewWith configuration: WKWebViewConfiguration,
|
|
|
+ for navigationAction: WKNavigationAction,
|
|
|
+ windowFeatures: WKWindowFeatures
|
|
|
+ ) -> WKWebView? {
|
|
|
+ guard let url = navigationAction.request.url else { return nil }
|
|
|
+
|
|
|
+ if RedditWebAuthHelper.isOAuthProviderURL(url) {
|
|
|
+ beginOAuthSignIn(url: url, in: webView)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ webView.load(URLRequest(url: url))
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+}
|