3

I have a client application, written in angular4 which communicates with a firebase server. Currently I'm trying to integrate a nodejs with firebase in a way that it will communicate with client app as well.

Firebase <----> Client (Angular4) <----> NodeJs(firebase-admin) <----> Firebase

I have setup a fire-admin configs:

var admin = require("firebase-admin");

var serviceAccount = require("./server/myfile.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://mydb.firebaseio.com"
});

and able to retrieve a data like this:

  var db = admin.database();
  var ref = db.ref("messages");

  ref.once("value", function(snapshot) {
    console.log(snapshot.val());
  });

But how can I check in NodeJS server whether user in client app has logged in ? I need this NodeJS server to communicate with client and provide data and files if neccessary.

1 Answer 1

3

Basically I solved an issue, following Firebase documentation:

If your Firebase client app communicates with a custom backend server, you might need to identify the currently signed-in user on that server. To do so securely, after a successful sign-in, send the user's ID token to your server using HTTPS. Then, on the server, verify the integrity and authenticity of the ID token and retrieve the uid from it. You can use the uid transmitted in this way to securely identify the currently signed-in user on your server.

On my Facebook Login in Angular4 App:

loginFb() {

       this.loading = true;

       this.afAuth.auth.signInWithPopup(new firebase.auth.FacebookAuthProvider()).then(

        (user) => {

            console.log('loginFB');
            console.log(user);

            var token = this.afAuth.auth.currentUser.getToken().then(
            (token) => console.debug(`******** Token: ${token}`)); <== TOKEN

            this.af.object(`/users/${user.uid}`).update({
                displayName: user.auth.displayName,
                email: user.auth.email,
                photoURL: user.auth.photoURL
            });

            this.router.navigate(['/']);

          }).catch(
            (err) => {
            this.loading = false;
            this.error = err;
        });
    }

My NodeJS Server:

var admin = require("firebase-admin");
var serviceAccount = require("./server/service-account.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://myapp.firebaseio.com"
});


...

//test method
router.get("/isAuth/:token", function (req, res) {

  var token = req.params.token;

  admin.auth().verifyIdToken(token)
  .then(function(decodedToken) {

    var uid = decodedToken.uid;
    console.log(`Current User ID: ${uid}`);

    }).catch(function(error) {

    console.log("Error: " + error);
  });


});

This is how authentication can be checked.

Sign up to request clarification or add additional context in comments.

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.