I use my nodejs server with passport to provide 3rd party auth. I setup a WebView on my Anrdroid client to load the following html:
<!DOCTYPE html>
<html>
<head>
<script src="file:///android_asset/oauth.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
With this javascript:
Android.callit('Loaded javascript file');
window.addEventListener('message', function(event) {
Android.callit('got message event');
authWindow.close();
}, false);
var authWindow = window.open("https://myserver/twitter/signin", "");
After successful auth to a third party server my nodejs server redirects to a page like this:
<html>
<head>
<script>window.opener.postMessage({"auth":true}, '*');</script>
</head>
<body>
</body>
</html>
Here is my Activity that starts the webview:
public class OAuthActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oauth);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.addJavascriptInterface(new JSInterface(), "Android");
webView.setWebViewClient(new WebViewClient() {
public void onReceivedSslError(WebView webView, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("file:///android_asset/oauth.html");
}
public class JSInterface {
@JavascriptInterface
public void callit(String with) {
Log.i("TEST", with);
}
}
}
So why is the 'message' event never firing?
As a note I have done this in a browser and it works fine. Just not when I try and use it in an android webview.