首页 > 解决方案 > 将 FirebaseAuth 的当前会话/令牌从本机 Android 应用程序传递到 WebView 的 Firebase 应用程序

问题描述

设想

我想将
通过FirebaseAuth
完成的 Android Native 应用程序的身份验证传递 到
嵌入Webview在我的应用程序中的 html/javascript 屏幕,这样我就可以调用我的 Firebase DB 而无需要求用户重新登录。

我尝试了什么我尝试
使用signInWithCustomToken 登录并使用从getidtokenresult生成的令牌,该令牌引发错误“INVALID_CUSTOM_TOKEN”

任何指导都会有所帮助

标签: firebasegoogle-cloud-firestorefirebase-authenticationandroid-webviewfirebase-security

解决方案


您可以在 Native 应用程序中使用 Firebase Admin SDK 为您的客户端应用程序生成一个有效令牌以用于signInWithCustomToken()

代币生成:

//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

在客户端应用程序中使用:

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

Firebase 文档中有关于此的更多信息


推荐阅读