Search code examples
firebasegoogle-cloud-firestorefirebase-authenticationandroid-webviewfirebase-security

Passing current session/token of FirebaseAuth from Native Android App to a WebView's Firebase App


Scenario

  • It is a hybrid app (Most of Android Native Java and part html/javascript in a WebView)
  • FirebaseAuth for authentication/signIn [in Android Native app]
  • Firestore as database
  • Serverless app

I want to
pass the Authentication from Android Native app which was done via FirebaseAuth
TO
html/javascript screens which are being embedded as Webview in my app, so that i can call my Firebase DB without asking user to relogin.

What I Tried
i tried to signIn using signInWithCustomToken and used the token generated from getidtokenresult which throws error "INVALID_CUSTOM_TOKEN"

Any guidance will be helpful


Solution

  • You can use the Firebase Admin SDK in your Native app to generate a valid token for your client app to use with signInWithCustomToken()

    Token Generation:

    //The uid should uniquely identify the user or device you are authenticating
    String uid = "some-uid";
    
    String customToken = FirebaseAuth.getInstance().createCustomToken(uid);
    // Send token back to client
    

    Use in client app:

    firebase.auth().signInWithCustomToken(token).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    });
    

    There is further information about this in the Firebase docs