0

I work on a personal project and I use Firebase (by Google) for the authentication of the users. The login works, but I want that the users can access some pages only if they'are connected.

I really don't know where to search.

Here's my code :

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        console.log("connecté");
        window.alert("Vous êtes bien connecté");
        window.location = 'home.html';

    } else {
        // No user is signed in.
    }
});

function login(){
    var userEmail = document.getElementById("mail").value;
    var userPassword = document.getElementById("password").value;

    firebase.auth().signInWithEmailAndPassword(userEmail, userPassword).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;

        window.alert("Error : " + errorCode + " " + errorMessage)
    });


}

function logout(){
    firebase.auth().signOut().then(function() {
        console.log("Déconnecté !");
        window.alert("Vous êtes bien déconnecté");
    }).catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;

        window.alert("Error : " + errorCode + " " + errorMessage)
    });
}

'''

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Anatole
  • 144
  • 8
  • You seem to be redirecting the user to `window.location = 'home.html';` already, once they're signed in. What do you want to change about this behavior? And what is the problem you have while implementing this? – Frank van Puffelen Sep 17 '19 at 05:28
  • @FrankvanPuffelen Thanks for you answer. I redirect the user to home.html, but i want to make this page invisible if they're not connected :) – Anatole Sep 17 '19 at 08:40
  • If you've deployed this to Firebase Hosting, there is no way to hide specific files. See https://stackoverflow.com/questions/27212004/can-firebase-hosting-restrict-access-to-resources – Frank van Puffelen Sep 17 '19 at 09:15

0 Answers0