From ff9b41b78dc3a7cfbab7829df7a52d6308b95463 Mon Sep 17 00:00:00 2001 From: Stefan Erkenberg Date: Fri, 6 May 2022 10:39:51 +0200 Subject: [PATCH] Send cancellation message when user cancels login --- src/android/OAuthPlugin.java | 16 +++++++++++++++- src/ios/OAuthPlugin.swift | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/android/OAuthPlugin.java b/src/android/OAuthPlugin.java index 21fcfef..fba56cb 100644 --- a/src/android/OAuthPlugin.java +++ b/src/android/OAuthPlugin.java @@ -58,7 +58,7 @@ public class OAuthPlugin extends CordovaPlugin { * The name of the package to use for the custom tab service. */ private String tabProvider = null; - + private boolean loginSuccessful = false; /** * Executes the request. @@ -121,6 +121,7 @@ public void onNewIntent(Intent intent) { } final String msg = jsobj.toString(); + loginSuccessful = true; this.webView.getEngine().evaluateJavascript("window.dispatchEvent(new MessageEvent('message', { data: 'oauth::" + msg + "' }));", null); } catch (JSONException e) { LOG.e(TAG, "JSON Serialization failed"); @@ -129,6 +130,19 @@ public void onNewIntent(Intent intent) { } } + @Override + public void onResume(boolean multitasking) { + super.onResume(multitasking); + // Chrome Custom Tabs currently have no way to detect that they were closed. + // But if this happens, the app resumes and onResume is called which can be used as workaround. + // If we didn't handle a successful oAuth response, we can assume that the login was cancelled in onResume. + if (!loginSuccessful) { + this.webView.getEngine().evaluateJavascript("window.dispatchEvent(new MessageEvent('message', { data: 'oauth::{\"error\":\"cancelled\"}' }));", null); + } else { + // reset loginSuccessful to make sure it is correctly initialized on the next login attempt + loginSuccessful = false; + } + } /** * Launches the custom tab with the OAuth endpoint URL. diff --git a/src/ios/OAuthPlugin.swift b/src/ios/OAuthPlugin.swift index 241a405..1f2ef98 100644 --- a/src/ios/OAuthPlugin.swift +++ b/src/ios/OAuthPlugin.swift @@ -37,6 +37,8 @@ class ASWebAuthenticationSessionOAuthSessionProvider : OAuthSessionProvider { self.aswas = ASWebAuthenticationSession(url: endpoint, callbackURLScheme: callbackURLScheme, completionHandler: { (callBack:URL?, error:Error?) in if let incomingUrl = callBack { NotificationCenter.default.post(name: NSNotification.Name.CDVPluginHandleOpenURL, object: incomingUrl) + } else if let err = error { + NotificationCenter.default.post(name: Notification.Name("oAuthCancellation"), object: err) } }) } @@ -64,6 +66,8 @@ class SFAuthenticationSessionOAuthSessionProvider : OAuthSessionProvider { self.sfas = SFAuthenticationSession(url: endpoint, callbackURLScheme: callbackScheme, completionHandler: { (callBack:URL?, error:Error?) in if let incomingUrl = callBack { NotificationCenter.default.post(name: NSNotification.Name.CDVPluginHandleOpenURL, object: incomingUrl) + } else if let err = error { + NotificationCenter.default.post(name: Notification.Name("oAuthCancellation"), object: err) } }) } @@ -135,6 +139,11 @@ class OAuthPlugin : CDVPlugin, SFSafariViewControllerDelegate, ASWebAuthenticati selector: #selector(OAuthPlugin._handleOpenURL(_:)), name: NSNotification.Name.CDVPluginHandleOpenURL, object: nil) + + NotificationCenter.default.addObserver(self, + selector: #selector(OAuthPlugin._handleError_(_:)), + name: Notification.Name("oAuthCancellation"), + object: nil) } @@ -210,6 +219,10 @@ class OAuthPlugin : CDVPlugin, SFSafariViewControllerDelegate, ASWebAuthenticati } + @objc internal func _handleError(_ notification : NSNotification) { + self.webViewEngine.evaluateJavaScript("window.dispatchEvent(new MessageEvent('message', { data: 'oauth::{\"error\":\"cancelled\"}' }));", completionHandler: nil) + } + @objc internal func _handleOpenURL(_ notification : NSNotification) { guard let url = notification.object as? URL else { return