如何在页面之间保留Firebase Auth实例

时间:2017-10-01 16:55:39

标签: javascript html firebase

我正在开展一个项目,我需要用他们的电子邮件和密码登录老师。或者使用他们的姓名,电子邮件和密码创建教师资料,然后使用firebaseUser.uid发送到firebase数据库。在验证它们之后,我将它们发送到他们的教师页面。

function init(){
    firebase.initializeApp(config);
    $('#submit').on('click', Login);
    $('#signUp').on('click', signup)
    $('#create').on('click', sendInfo);
}

function Login(){
    var email = $('#emailI').val();
    var pass = $('#passI').val(); 

    var promise = firebase.auth().signInWithEmailAndPassword(email,pass);
    promise.catch(e => console.log(e.message));

    firebase.auth().onAuthStateChanged(firebaseUser =>{
        if(firebaseUser){
            console.log(firebaseUser);
            setTimeout(replace, 2500);
        }else{
            console.log("not logged in");
        }
    });
}

代码续......

function sendInfo(){
        //Get information values
        var email = $('#email').val();
        var pass = $('#pass').val();
        var name = $('#name').val();

        //Create user with createUserWithEmailAndPassowrd();
        var promise = firebase.auth().createUserWithEmailAndPassword(email,pass);
        promise.catch(e => console.log(e.message));

        stateChanged(email,pass,name);
    }

function stateChanged(email,pass,name){
    firebase.auth().onAuthStateChanged(firebaseUser =>{
        if(firebaseUser){
            console.log(firebaseUser);
            //Create Firebase database ref
            var ref = firebase.database().ref('Teachers');
            ref.child(firebaseUser.uid).set({
                Name:name,
                Email:email,
                Passowrd:pass,
            });
            setTimeout(replace, 2500);
        }else{
            console.log("Not Logged In");
        }

    });
}

function replace(name){
    location.replace("teacherPage.html?room=" + name);
}

项目的下一部分是在特定教师页面的顶部创建一个h1,其名称来自firebase

//Pull teacher name form Firebase 
    var pullName = firebase.database().ref("Teachers").child(firebaseUser.uid);
    pullName.once('value', function(snapshot){
        data = snapshot.val();
        name = data.Name;
        console.log(name);

     var teachName = document.createElement("h1");
        teachName.textContent = name;
        teachName.className = "tName";

        document.getElementById("firstHead").appendChild(teachName);
    });

这些只是我遇到麻烦的主要部分。很多此项目取决于每个页面上的Auth Instance。我在firebaseUser.uid下保存了所有特定的教师信息。但是,google chrome中的控制台说firebaseUser未定义。我该如何解决这个问题?

0 个答案:

没有答案