所以我在进一步开发这个网站,这次我增加了一个firebase登录,登录后应该会重定向到主页面。然而我每次使用window.location方法都会得到 "This site can't provide a secure connection, it sent an invalid response"。一旦我把这行注释掉,一切都能正常工作。我翻遍了所有stackoverflow相关的问题和js文档,似乎找不到答案,所以任何帮助都将是非常好的。
var firebaseConfig = {
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
const auth = firebase.auth();
//Get Elements
const txtEmail = document.getElementById('txtEmail');
const txtPassword = document.getElementById('txtPassword');
const btnLogin = document.getElementById('btnLogin');
const btnSignUp = document.getElementById('btnSignUp');
const btnLogOut = document.getElementById('btnLogOut');
btnLogOut.style.display= 'none'
//Add login event
btnLogin.addEventListener('click', e => {
event.preventDefault()
const email = txtEmail.value;
const pass = txtPassword.value;
//Sign In
try {
auth.signInWithEmailAndPassword(email, pass);
} catch (error) {
console.log(error.message);
}
})
//Add SignUp Event
btnSignUp.addEventListener('click', e => {
const email = txtEmail.value;
const pass = txtPassword.value;
//Sign up
try {
auth.createUserWithEmailAndPassword(email, pass);
} catch (error) {
console.log(error.message);
}
})
btnLogOut.addEventListener('click', e => {
event.preventDefault()
firebase.auth().signOut();
});
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser) {
window.location.replace('https://www.camisite.alenieto97.now.sh/home.html')
btnLogin.style.display = 'none'
btnSignUp.style.display = 'none'
btnLogOut.style.display = ''
} else {
console.log('not logged in')
btnLogOut.style.display = 'none'
btnLogin.style.display = ''
btnSignUp.style.display = ''
}
我删除了firebase.config的每一个阵营。
你有试过用root级别的url吗?这是因为如果你的应用程序是托管在 http://localhost
或任何其他域名,然后你试图改变的网址,以 https://www.camisite.alenieto97.now.sh
它将抛出 SECURITY_ERROR
. 使用root级别的url就可以解决这种问题。此外,你为什么要硬编码的域名,可能会改变后?
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser) {
window.location.replace('/home.html')
btnLogin.style.display = 'none'
那么Mkam是对的。url是错误的。它的所有工作了罚款后,删除 "www. "的网址。