Reputation: 149
I'm building a webapp (html, css, js) with Firebase backend. If the site is opened from the browser, everything is working well, if the user is logged in, it redirects the user to another page. However if I'm using the same site from an android app (which is using webview), the redirecting doesn't work. Any idea why?
The code I'm using in android studio:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
});
web.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
web.loadUrl("http://mywebsite.com/login");
web.getSettings().setAppCacheEnabled(false);
web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
web.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
}
And this is how the redirecting works:
function loginUser() {
emailVal = document.getElementById("email").value;
passwordVal = document.getElementById("password").value;
firebase.auth().signInWithEmailAndPassword(emailVal, passwordVal)
.then((userCredential) => {
var user = userCredential.user;
})
.catch((error) => {
});
}
firebase.auth().onAuthStateChanged((user) => {
if (user) {
window.location.replace('http://mywebsite.com/home');
}
});
Upvotes: 1
Views: 1293
Reputation: 149
So, after having some google search, I've found this StackOverflow topic: firebase google authentication in web does not work on android app webview
The problem was that I hadn't enabled the DOM storage. By inserting these two lines to my Java code solved the problem:
WebSettings webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true); // localStorage
Upvotes: 3