繁体   English   中英

登录js和firebase后如何将用户重定向到另一个页面?

[英]How do I redirect the user to another page after logging in js and firebase?

我在识别将用户重定向到其他页面的已用代码时遇到问题,因为我从 Internet 复制了代码,但是我在登录后尝试重定向用户时遇到了问题; 我需要确保用户登录不是因为他有链接,所以他可以看到我想指出的页面我使用 firebase.google.com 作为检查用户的一种方式,这里是我的 app.js 文件

 firebase.auth().onAuthStateChanged(function(user) { if (user) { // User is signed in. document.getElementById("user_div").style.display = "block"; document.getElementById("login_div").style.display = "none"; var user = firebase.auth().currentUser; if(user != null){ var email_id = user.email; document.getElementById("user_para").innerHTML = "Welcome:" + email_id; } } else { // No user is signed in. document.getElementById("user_div").style.display = "none"; document.getElementById("login_div").style.display = "block"; } }); function login(){ var userEmail = document.getElementById("email_field").value; var userPass = document.getElementById("password_field").value; firebase.auth().signInWithEmailAndPassword(userEmail, userPass).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; window.alert("Error : " + errorMessage); // ... }); } function logout(){ firebase.auth().signOut(); } auth.onAuthStateChanged(function(user){ if(user){ //is signed in }else{ alert("you are not logged in"); //not signed in } });
 *{ direction: rtl; } body { background: #fff; padding: 0px; margin: 0px; font-family: 'Nunito', sans-serif; font-size: 16px; } input, button { font-family: 'Nunito', sans-serif; font-weight: 700; } .logo{ display: block; margin-left: auto; margin-right: auto; width: 128px; height: 128px; } .main-div, .loggedin-div { width: 20%; margin: 0px auto; margin-top: 150px; padding: 20px; display: none; } .main-div input { display: block; border: 1px solid #ccc; border-radius: 5px; background: #fff; padding: 15px; outline: none; width: 100%; margin-bottom: 20px; transition: 0.3s; -webkit-transition: 0.3s; -moz-transition: 0.3s; } .main-div input:focus { border: 1px solid #777; } .main-div button, .loggedin-div button { background: #5d8ffc; color: #fff; border: 1px solid #5d8ffc; border-radius: 5px; padding: 15px; display: block; width: 100%; transition: 0.3s; -webkit-transition: 0.3s; -moz-transition: 0.3s; } .main-div button:hover, .loggedin-div button:hover { background: #fff; color: #5d8ffc; border: 1px solid #5d8ffc; cursor: pointer; }
 <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="style.css"> <link rel="shortcut icon" href="images\\favicon.ico" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700" rel="stylesheet"> </head> <body> <div id="login_div" class="main-div"> <img src="./images/logo.png" alt="logo" class="logo"> <input type="email" placeholder="email" id="email_field" /> <input type="password" placeholder="password" id="password_field" /> <button onclick="login()">login</button> </div> <div id="user_div" class="loggedin-div"> <h3>welcome</h3> <p id="user_para">Welcome to Firebase web login Example. You're currently logged in.</p> <button onclick="logout()">logout</button> </div> <script src="https://www.gstatic.com/firebasejs/4.8.1/firebase.js"></script> <script> // Your web app's Firebase configuration var firebaseConfig = { apiKey: "******************************", authDomain: "******************************", databaseURL: "******************************", projectId: "******************************", storageBucket: "******************************", messagingSenderId: "******************************", appId: "******************************" }; // Initialize Firebase firebase.initializeApp(firebaseConfig); </script> <script src="app.js"></script> </body> </html>
我需要你的帮助,请:)

auth.onAuthStateChanged(function(user){
  if(user){
    // is signed in
    window.location.href = 'https://www.......';
  } else {
    // not signed in
    alert("you are not logged in");
  }
});

如何 - 重定向到另一个网页

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM