javascript - 成功登录 Firebase 后如何重定向页面?
问题描述
我正在尝试在使用 Firebase 身份验证成功登录和/或注册后将用户重定向到新页面。
我尝试按照 Firebase 提供的文档代码进行操作,但会导致无限循环被重定向到同一页面。
我认为问题可能在于我如何调用 initApp() 函数。
/**
* Handles the sign in button press.
*/
function handleLogIn() {
if (firebase.auth().currentUser) {
// [START signout]
firebase.auth().signOut();
console.log("We have signed out :(");
window.location = 'login.html';
// [END signout]
} else {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Sign in with email and pass.
// [START authwithemail]
firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
console.log(error);
// [END_EXCLUDE]
});
// [END authwithemail]
}
//successful log in
initApp();
}
/**
* Handles the sign up button press.
**/
function handleSignUp() {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Sign in with email and pass.
// [START createwithemail]
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
// [END_EXCLUDE]
});
// [END createwithemail]
}
/**
* initApp handles setting up UI event listeners and registering Firebase auth listeners:
* - firebase.auth().onAuthStateChanged: This listener is called when the user is signed in or
* out, and that is where we update the UI.
*/
function initApp() {
// Listening for auth state changes.
// [START authstatelistener]
firebase.auth().onAuthStateChanged(function (user) {
// [START_EXCLUDE silent]
//document.getElementById('quickstart-verify-email').disabled = true;
// [END_EXCLUDE]
if (user) {
// User is signed in.
window.location = 'index.html';
var displayName = user.displayName;
var email = user.email;
console.log("Houston we are logged in! " + email);
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
// [START_EXCLUDE]
if (!emailVerified) {
}
// [END_EXCLUDE]
} else {
// User is signed out.
window.location = 'login.html';
// [START_EXCLUDE]
// [END_EXCLUDE]
}
// [START_EXCLUDE silent]
// [END_EXCLUDE]
});
// [END authstatelistener]
}
//check state change
window.onload = function () {
initApp();
};
解决方案
推荐阅读
- c++ - 参考和朋友类的意外行为
- sh - 当键可以深度嵌套时,jq 使用嵌套 json 中的键获取值
- javascript - Chart-JS:x 轴下的表格
- python - Python docx AttributeError:'WindowsPath' 对象没有属性'seek'
- oracle - PlSQL 动态查询以在时间戳上透视数据
- node.js - 续集中在同一张表上的左连接
- android - 支持 Android Pie 中的浅色和深色主题
- java - java编译器是JVM的一部分吗?
- upgrade - 自由半径升级问题
- java - onNext 中未捕获的异常被传播到 RxJava 全局异常处理程序