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?

1
  • 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? Commented Jul 21, 2017 at 5:23

3 Answers 3

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);
    });
});
Sign up to request clarification or add additional context in comments.

7 Comments

Error - promise.reject is not a function
@NomanAli Make sure you've used Promise.reject and not promise.reject.
with Promise is says in console ReferenceError: $http is not defined
@NomanAli Make sure you've imported libraries properly. If you're using angularjs you may need to inject $http service in your controller.
Sorry! i am using vanilla JS, not angular. :(
|
0

Your route needs to respond:

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

Comments

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*/
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.