admin管理员组文章数量:1323317
I am creating a website that has login with firebase google authentication. It's working fine in all browsers. But when I add this website in my app as webview it does not work.
website showing this error:
This operation is not supported in the environment this application is running on. "location.protocol" must be http, https or chrome-extension and web storage must be enabled.
here some code bellow:
javascript code:
function login(){
console.log('login called');
function newLoginHappend(user){
if(user){
model_questions(user);
}else{
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}
}
firebase.auth().onAuthStateChanged(newLoginHappend);
}
window.onload = login();
webview code:
WebSettings webSettings =webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("/");
Is there any way or technique to solve this problem?? if you have any idea then share with us please.
thanks
I am creating a website that has login with firebase google authentication. It's working fine in all browsers. But when I add this website in my app as webview it does not work.
website showing this error:
This operation is not supported in the environment this application is running on. "location.protocol" must be http, https or chrome-extension and web storage must be enabled.
here some code bellow:
javascript code:
function login(){
console.log('login called');
function newLoginHappend(user){
if(user){
model_questions(user);
}else{
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}
}
firebase.auth().onAuthStateChanged(newLoginHappend);
}
window.onload = login();
webview code:
WebSettings webSettings =webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://mahmud-cse16.github.io/CBAP_Handout/");
Is there any way or technique to solve this problem?? if you have any idea then share with us please.
thanks
Share edited Aug 18, 2020 at 13:49 now 5,0373 gold badges28 silver badges27 bronze badges asked Jul 6, 2019 at 15:45 Md Mahmudul IslamMd Mahmudul Islam 2,7325 gold badges20 silver badges33 bronze badges 2- enable DOM storage and check if the problem persists, webSettings.setDomStorageEnabled(true); – BLACKMAMBA Commented Jul 7, 2019 at 6:04
- 1 Thank's it works. after trying this i am getting disallowed_useragent error. then I am fixed it. – Md Mahmudul Islam Commented Jul 8, 2019 at 4:35
2 Answers
Reset to default 5Try enabling DOM Storage for the webview
WebSettings webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true); // localStorage
Sets whether the DOM storage API is enabled. The default value is false.
Android Developer Reference
To get around "disallowed_useragent" error, one way is to use the Android Google Auth SDK to log in natively in the app, and then pass the Google token to the Webview so Firebase can use it with auth.signInWithCredential()
.
It might work for other providers but here's how I did it with Google auth on Android:
Android:
val gso =
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(clientId)
.requestEmail()
.build()
val googleSignInClient = GoogleSignIn.getClient(activity, gso)
activity.startActivityForResult(
googleSignInClient.signInIntent,
object : ActivityWithResultListener.OnActivityResultListener {
override fun onActivityResult(resultCode: Int, data: Intent?) {
if (data != null) {
val credential = GoogleAuthProvider.getCredential(account.idToken!!, null)
idToken = account.idToken!!
// Then pass the token to your webview via a JS interface
}
}
}
)
Web:
const idTokenFromApp = AndroidBridge.getAuthToken(); // This is the idToken from the Android code above
if (idTokenFromApp) {
// from https://firebase.google./docs/auth/web/google-signin
// Build Firebase credential with the Google ID token.
// https://firebase.google./docs/reference/js/v8/firebase.auth.GoogleAuthProvider#static-credential
const credential = firebase.auth.GoogleAuthProvider.credential(idTokenFromApp);
// Sign in with credential from the Google user.
firebase.auth.signInWithCredential(credential).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
logError(`Error logging in: ${errorCode} ${errorMessage} token: ${idTokenFromApp}`, error);
});
}
本文标签: javascriptfirebase google authentication in web does not work on android app webviewStack Overflow
版权声明:本文标题:javascript - firebase google authentication in web does not work on android app webview - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742144752a2422740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论