0

I am trying to learn how to make a simple authentication application. If I have this in my app.js file,

var logIn = function(req, res) {
  const email = req.body.email;
  const password = req.body.password;
  firebase.auth().signInWithEmailAndPassword(email, password).then(function() {
    const user = firebase.auth().currentUser;
    const emailVerified = user.emailVerified;
    console.log("Email verified status: " + emailVerified);
 });
};
var doSomething(req, res) {
  var user = firebase.auth().currentUser;
  // do something with the variable user
}

this is not ok, right? Because when multiple users log in, the currentUser would just be assigned to the last person who logged in. (Please correct me if that is not true.)

So, I found these:

How to authenticate a firebase user from server without client side auth?

https://firebase.google.com/docs/auth/web/auth-state-persistence

They suggested doing something like this:

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE)
.then(function() {
  return firebase.auth().signInWithRedirect(provider);
});

Does this mean, that adding setPersistence = None allows us to use firebase.auth().currentUser on the server-side, AKA in app.js, even when multiple users are logged in?

Can we just declare firebase.auth().setPersistence(firebase.auth.Auth.Persistence.NONE) once in the app.js file or do we need to use promise for each function?

I recently started learning JS and using Firebase. Sorry if some questions are silly. Thanks so much for your help.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Student
  • 41
  • 7
  • 1
    Why are you trying to use Firebase Authentication server-side? Generally you won't want to do this -- you'll want to use it client-side and pass the token back to the server. Unless you have a very strong reason, that's what I'd recommend you do. – Michael Bleigh Jan 13 '20 at 22:58
  • "Because when multiple users log in, the currentUser would just be assigned to the last person who logged in." The premise of Firebase Authentication is that (at most) a single user is signed in to your application. If you have a different needs, you might want to consider if Firebase Authentication the the best solution for your app. – Frank van Puffelen Jan 14 '20 at 00:06

0 Answers0