lundi 30 mai 2016

firebase.initializeApp callback/promise?

This is my web page

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Calcolo Diliuzioni</title>

    </head>
    <body>

        <h1>My app</h1>

        <!-- Libreria per gestire l'autenticazione con google -->
        <script src="http://ift.tt/1bMh26W" async defer></script>

        <!-- Firebae config -->
        <script src="http://ift.tt/24gI965"></script>
        <script>
        // Initialize Firebase
        var config = {
        apiKey: "",
        authDomain: "",
        databaseURL: "",
        storageBucket: "",
        };
        firebase.initializeApp(config);
        </script>

        <script type="text/javascript">
            var user = firebase.auth().currentUser;
            if (user) {
                console.log(user);
            } else {
                console.log(user);
            }
        </script>
    </body>
</html>

Let's assume I have an already logged user. When I load the page in the console I got null. While I'm expecting to have the current user's object.

If I run the same code in the browser console

var user = firebase.auth().currentUser;
if (user) {
    console.log(user);
} else {
    console.log(user);
}

I'm able to get the current user's object.

I think that the firebase.initializeApp(config); has some async behavior. What's the right way to work around it? Should I use a promise or something on the firebase.initializeApp(config); function? A sort of callback..




Aucun commentaire:

Enregistrer un commentaire