我正在使用firebase身份验证但是当我被重定向到其他页面时我没有得到(uid)

时间:2017-01-25 06:32:42

标签: javascript html firebase

当我使用我的凭据登录时,我遇到了一个独特的问题我在我的控制台上获得了用户ID但是我正在尝试做的是将用户重定向到信息页面,我做的与我在siginup页面中做的相同但不是直接获得UID。加上当我刷新页面时,它显示我正确的UID。我很困惑任何帮助都会很棒,这是我的代码

**Information.html**
<!DOCTYPE html>
<html lang="en">

    <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>Information</title>

        <!-- CSS -->
        <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
        <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="assets/css/form-elements.css">
        <link rel="stylesheet" href="assets/css/style.css">
    <!--Cloudinary imports -->
        <!-- ends here -->
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    <script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
     <script src="info.js"></script>

     </head>

<body>
<script src ="index.js"></script>
<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li><a href="user_homepage.html">DashBoard</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="home.html"><span class="glyphicon glyphicon-log-out"></span> Login Out</a></li>
      </ul>
    </div>
  </div>
</nav>
        <!-- Top content -->
        <div class="top-content">
            <div class="inner-bg">
                <div class="container">
                <div class="row">
                        <div class="col-sm-6 col-sm-offset-3 form-box">

                            <div role="form"  class="registration-form">

                                <fieldset>
                                    <div class="form-top">
                                        <div class="form-top-left">
                                            <h3>Information Cell</h3>
                                            <p>Tell us who you are:</p>
                                        </div>
                                        <div class="form-top-right">
                                            <i class="fa fa-user"></i>
                                        </div>
                                    </div>
                                    <div class="form-bottom">
                                        <div class="form-group">
                                            <label class="sr-only" for="form-first-name">First name</label>
                                            <input type="text" id="name" placeholder="Name..." class="form-first-name form-control" >
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-last-name">Class</label>
                                        </div>
                                        <div>
                                        <select class="form-control">
                                        <option >Select One!</option>
                                        <option id="userClass" >XI</option>
                                        <option id="userClass">XII</option>
                                      </select>
                                    </div>
                                        <div class="form-group">
                                        <label class="sr-only" for="form-facebook">Select Your Subjects</label>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Physics">Physics</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Chemistry">Chemistry</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Maths">Maths</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Botany">Botany</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Zoology">Zoology</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Islamiat">Islamiat</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Urdu">Urdu</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="English">English</label>
                                        </div>
                                        <div class="checkbox">
                                        <label><input type="checkbox" value="Pakistan Studies">Pakistan Studies</label>
                                        </div>
                                        </div>
                                        <div class="form-group">
                                         <input type='file' id= "profileImage" class=" btn btn-primary" onchange="readURL(this);" />
                                         <img id="blah" name="profile Image" alt="Loading!!" class="img-circle"/>
                                        </div>
                                        <button  type="submit" id="write" onclick="write()" class="btn">Set up Complete</button>
                                    </div>
                                    </div>
                                </fieldset>                    
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>        
        <!-- Javascript -->
        <script>
        function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah')
                    .attr('src', e.target.result)
                    .width(150)
                    .height(150);
            };

            reader.readAsDataURL(input.files[0]);
        }
    }

        </script>
        <script src="assets/js/jquery-1.11.1.min.js"></script>
        <script src="assets/bootstrap/js/bootstrap.min.js"></script>
        <script src="assets/js/jquery.backstretch.min.js"></script>
        <script src="assets/js/retina-1.1.0.min.js"></script>
        <script src="assets/js/scripts.js"></script>

        <!--[if lt IE 10]>
            <script src="assets/js/placeholder.js"></script>
        <![endif]-->
  <script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
<script>
  //firebase credentials
  firebase.initializeApp(config);
</script>
</body>
<footer class="container-fluid text-center">

</footer>
</html>


**info.js**
function write(){
    function writeUserData(name, userClass, profileImage) {
      firebase.database().ref('userDataNode/' + user.uid).set({
        userName: name,
        userClass: userClass,
        profileImage : profileImage

  });
  document.getElementById('name').textContent = 'name';
  document.getElementById('userClass').textContent = 'userClass';
  document.getElementById('profileImage').textContent = 'profileImage';
  document.getElementById('write').textContent = 'write';

}
}

 function initApp() {
    firebase.auth().onAuthStateChanged(function(user) {
    console.log (user.uid);
    }); 
 }

window.onload = function() {
     // Initialize Firebase  
      initApp();
    };

**index.js**

//Login Method Javascript
function CreateUser(){
    var email = document.getElementById('txtEmail').value;
        var password = document.getElementById('txtPassword').value;
    firebase.auth().createUserWithEmailAndPassword(email, password)

    .catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  if (errorCode == 'auth/weak-password') {
    alert('The password is too weak.');
  } else {
    alert(errorMessage);
     document.getElementById('Sign Up').disabled = false;
  }
  console.log(error);
   document.getElementById('Sign Up').disabled = true;
});

alert("your Account has been created");
 document.getElementById('Sign Up').textContent = 'CreateUser';
}
function login(){
    var email = document.getElementById('txtEmail').value;
        var password = document.getElementById('txtPassword').value;
    firebase.auth().signInWithEmailAndPassword(email, password)

    .catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  if (errorCode == 'auth/weak-password') {
    alert('The password is too weak.');
  } else {
    alert(errorMessage);
     document.getElementById('logged In').disabled = false;
  }
  console.log(error);
   document.getElementById('logged In').disabled = true;
});

 alert("Logged In");
 document.getElementById('logged In').textContent = 'login';
 window.open("information.html");
}
function logOut(){
firebase.auth().signOut().then(function() {
  // Sign-out successful.

  alert("Logged Out");
  document.getElementById('logged Out').disabled = true;
}, function(error) {
  document.getElementById('logged Out').disabled = false;
});
}

function write(){
    function writeUserData(name, userClass, profileImage) {
      firebase.database().ref('userDataNode/' + user.uid).set({
        userName: name,
        userClass: userClass,
        profileImage : profileImage

  });
  document.getElementById('name').textContent = 'name';
  document.getElementById('userClass').textContent = 'userClass';
  document.getElementById('profileImage').textContent = 'profileImage';
  document.getElementById('write').textContent = 'write';
}
}

 function initApp() {
    firebase.auth().onAuthStateChanged(function(user) {
    console.log (user.uid);
    }); 
 }

 window.onload = function() {
     // Initialize Firebase  
      initApp();
    };

**signin.html**

<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Sign In</title>
  <link rel="stylesheet" href="css/style.css">
  <!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  <script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
  <script src="index.js"></script>
</head>
<body>
  <div class="sign-up">
    <h1 class="sign-up-title">Sign In</h1>
    <p>Don't Have an account?<a href="index.html">Sign up</a></p><br/>
    <input id="txtEmail" type="email" class="sign-up-input" placeholder="Enter Email" autofocus>
    <input  id="txtPassword" type="password" class="sign-up-input" placeholder="Enter password">
    <button id="logged In" onclick="login()" class="sign-up-button">Sign In!</button><br/><br/>
    <button id ="logged Out" onclick="logOut()" class="sign-up-button">Sign Out!</button>
  </div>
  <script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
<script>
  // Initialize Firebase
  //firebase credentials 
  firebase.initializeApp(config);
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以将UID保存为会话Cookie!

在注册页面上设置如下:

sessionStorage.setItem('uid', firebase.auth().currentUser.uid);

阅读其他页面:

sessionStorage.getItem('uid');