Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

signInWithCustomToken() with Firebase's own tokens

I want to authenticate users on multiple websites via sharing the JWT token. The token was generated originally by firebase, so I assume it is a good and valid token (tested on jwt.io and seems to be fine). I get it by user.getToken().

As I call signInWithCustomToken(token) I always get an error auth/invalid-custom-token with a message "The custom token format is incorrect. Please check the documentation.".

What is strange, it does a HTTP POST request to https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken... which returns HTTP 400. I have tried on both localhost and also on the https live website... but same result. I'm using Firebase 3.3.

Any ideas, what could be wrong? I was using this feature with the old Firebase.

like image 859
Tom Avatar asked Aug 23 '16 20:08

Tom


People also ask

What is signInWithCustomToken?

Signs in or signs up a user by exchanging a custom Auth token. Upon a successful sign-in or sign-up, a new Identity Platform ID token and refresh token are issued for the user. An API key is required in the request in order to identify the Google Cloud project.

What is custom token authentication?

What Are Custom Authentication Tokens? An authentication token is some data, represented as a string or XML, that identifies an entity (user or process), such as an X509 client certificate. Typically, authentication tokens are designed to be used within specific security protocols.

How do I use tokens in firebase?

Firebase gives you complete control over authentication by allowing you to authenticate users or devices using secure JSON Web Tokens (JWTs). You generate these tokens on your server, pass them back to a client device, and then use them to authenticate via the signInWithCustomToken() method.


1 Answers

You don't provide any code snippets so i am gonna speculate here.

I am assuming you are using node.js to generate the custom token. Try this in your node script:

var firebase = require("firebase");
var adminConfig = {
    serviceAccount: "serviceAccountCredentials.json"
};
var adminApp = firebase.initializeApp(adminConfig, 'admin');
var token = adminApp.auth().createCustomToken('12345678', {
    customField: 'customValue'
});

Then on the client try to signInWithCustomToken(token) Make sure the client is using the same api key that belongs to the same project where the service account was generated.

like image 105
bojeil Avatar answered Jan 02 '23 10:01

bojeil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!