• Breaking News

    How do I detect if a user is already logged in Firebase?

    What are the most common Firebase pitfalls and gotchas? I would say how to detect if a user is already logged in,  by far.



    As of May 2018, you can easily detect if the user is logged or not by executing:

    var user = firebase.auth().currentUser;

    IMPORTANT: You need to wait for the firebase call to complete, otherwise, you will face a very common issue: the function returns "null" as a response.

    Use case

    Let's suppose you perform the login action on Page A and then you invoke Page B, on Page B you can call the following JS code to test the expected behavior:


    You can grab the code from here: Firebase Auth Example

    If the user is logged then "var user" will contain the expected JSON payload, if not, then it will be just "null"

    And that's all you need.

    No comments