How to share Authentication token between app in Firebase 3.0?

好久不见. 提交于 2021-02-07 14:44:38

问题


In older version of Firebase we can obtain authenticated token for sharing between our app, extensions.

But when upgrade to Firebase 3.0, that function doesn't work anymore. I've use getTokenWithCompletion: on FIRUser, then in my App Extension I call signInWithCustomToken:completion: with token i just obtained. But Firebase return an error with messgage:

Error Domain=FIRAuthErrorDomain Code=17000 "The custom token format is incorrect. Please check the documentation." UserInfo=0x799b6010 {error_name=ERROR_INVALID_CUSTOM_TOKEN, NSLocalizedDescription=The custom token format is incorrect. Please check the documentation.}

How to get authenticate token from FIRUser to re-authenticate it in my App Extension?


回答1:


signInWithCustomToken is meant to be used with your own tokens minted on your own server (read more here).

The best way for you to bootstrap a session between different components of your application will be to, at sign-in time, share the user's credential and perform the bootstraping across all your components.

For example, if you are using Facebook login, when your retrieve the facebook access token, you would have to share it from your app to your extension, and then call signInWithCredential with the same token in both your main app and extension.

There is currently no way to sign in a user into a Firebase app with the v3.X SDKs from another Firebase app instance.




回答2:


I had the same problem this morning when upgrading to the latest version of Firebase on Android. To fix the problem I had to update the Firebase Server SDK to version 3.0+

This is a Java backend implementation, but the same applies for NodeJS as well.

<dependency>
  <groupId>com.google.firebase</groupId>
  <artifactId>firebase-server-sdk</artifactId>
  <version>[3.0.0,)</version>
</dependency>

In the new Firebase Server SDK you have to initialize your Firebase app first using a generated JSON file.(Found under permissions in your new Firebase console) Then you can generate the JWT token.

FirebaseOptions options = new FirebaseOptions.Builder()
                .setServiceAccount(new FileInputStream("path/to/json/file.json"))
                .setDatabaseUrl("https://myapp.firebaseio.com/")
                .build();

FirebaseApp.initializeApp(options);

String token = FirebaseAuth.getInstance().createCustomToken("userID");

A token generated this way worked allowed me to use the new signInWithCustomToken() method.

You can read more here: https://firebase.google.com/docs/auth/server

Hope this helps




回答3:


I had the same error as the OP. I am assuming that you are using a service account email and private key during token generation on your server. The token my server generated was successful on http://jsfiddle.net/firebase/XDXu5/. However, my app would give the same error.

In the end, it turned out that I was signing it using "HS256" (which is what I was using in my existing token generation - pre firebase 3.0). When I changed it to "RS256" instead, the token generated works in the app.

Hope that helps.



来源:https://stackoverflow.com/questions/37337714/how-to-share-authentication-token-between-app-in-firebase-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!