简体   繁体   中英

firebase google authentication in web does not work on android app webview

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

Try 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.com/docs/auth/web/google-signin
    // Build Firebase credential with the Google ID token.
    // https://firebase.google.com/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);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM