0

I have been fighting with an (firebase & nodejs) issue and it's 4th part of that problem, How can i pass data after login to server.js

I have tried this But failed to get it working.

Basically i am trying to send user idToken to server to get it verified.

This what i have tried:

const promise = auth.signInWithEmailAndPassword(email, pass).then(function(){
    $('.load-bar').hide();
    firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
        // Send token to your backend via HTTPS
        console.log(data);
        $http.get('server', {params: {idToken: idToken}})
            .success(
                function(success){
                    console.log('success');
                })
            .error(
                function(error){
                    console.log(error)
                });
    }).catch(function(error) {
      // Handle error
    });
});

But it's doing nothing no error not success and on server.js

app.get('/server', function(req,res, next){
   console.log(req.query.idToken);
});

But still nothing. What am i doing wrong?

Noman Ali
  • 3,160
  • 10
  • 43
  • 77
  • Is the `console.log(req.query.idToken);` called on the server and does it output something? If not is the `console.log(data)` called on the client? Is there an error reported at `// Handle error`? – t.niese Jul 21 '17 at 05:23

3 Answers3

1

If you don't return Promise from then, the Promise will return undefined. And to resolve a promise with success you need to return Promise.resolve and to terminate with error you need to return Promise.reject. Try this -

const promise = auth.signInWithEmailAndPassword(email, pass).then(function(){
    $('.load-bar').hide();
    return firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
        // Send token to your backend via HTTPS
        console.log(data);
        $http.get('server', {params: {idToken: idToken}})
            .success(
                function(success){
                    return Promise.resolve(success);
                })
            .error(
                function(error){
                    return Promise.reject(error);
                });
    }).catch(function(error) {
      return Promise.reject(error);
    });
});
shawon191
  • 1,945
  • 13
  • 28
0

Your route needs to respond:

app.get('/server', function(req,res, next){
  console.log(req.query.idToken);
  return res.send('success!');
});
Ezra Chu
  • 832
  • 4
  • 13
0

The issue is you are not sending your response back to the api call origin. That is why you are not getting success or error results.

Change the code in server as:

app.get('/server', function(req, res, next){
   console.log(req.query.idToken);
   res.send(200);/*Just sends success code*/
   //res.send("Token rcvd");/*Or send some custom message back to call origin*/
});