Skip to content

Commit

Permalink
Send cancellation message when user cancels login
Browse files Browse the repository at this point in the history
  • Loading branch information
erkenberg committed May 15, 2022
1 parent a09b837 commit ff9b41b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/android/OAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand All @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/ios/OAuthPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ff9b41b

Please sign in to comment.