Shubham Gupta
Shubham Gupta

Reputation: 434

How to send custom tokens from node.js server to android client

I am using node.js as my backend server for my android application. I am receiving the id-token from http request and from them i generate custom tokens,(I am using this link: https://firebase.google.com/docs/auth/admin/create-custom-tokens) but I am stuck on how to send back the custom tokens to my android client.

app.get("/id-tokens/:token", function(req,res){
  // Receiving id-tokens

  var idToken = req.params.token;

  admin.auth().verifyIdToken(idToken)
    .then(function(decodedToken) {
      var uid = decodedToken.uid;
    }).catch(function(error) {
      console.log("Error receiving tokens");
    });


  admin.auth().createCustomToken(uid)
    .then(function(customToken) {
      // Send token back to client?? HOW??
    })
    .catch(function(error) {
      console.log("Error creating custom token:", error);
    });

});

Can FCM(Firebase Cloud messaging) be used here? I want to avoid using http method due to security reasons. Can socket, FCM or any safer method be used in its place? I am new to android so any help would be appreciated... :)

Upvotes: 0

Views: 680

Answers (1)

VictorArcas
VictorArcas

Reputation: 630

app.get("/id-tokens/:token", function(req,res){
  var idToken = req.params.token;

  admin.auth().verifyIdToken(idToken)
    .then(function(decodedToken) {
      var uid = decodedToken.uid;

    admin.auth().createCustomToken(uid) // you need to do this call once you have the value for uid
        .then(function(customToken) { // otherwise, uid will be undefined
            res.send(customToken) // since you are using http, just return the token like this
        })
        .catch(function(error) {
        console.log("Error creating custom token:", error);
        });
    }).catch(function(error) {
      console.log("Error receiving tokens");
    });
});

Upvotes: 1

Related Questions