HELP! 我不知道发生了什么,但我的登录页面不起作用。即使我输入有效的用户/密码,它也会重新加载。 我认为问题是我的数据结构,安全规则和app.js问题陷入困境,但我不知所措。 我被提供了一个sinatra / ruby简单api与用户合作&小组(只是一个小项目)。
这里的网站: https://starter-vicks9985.firebaseapp.com/index.html
这里是代码:
$.post/("https://starter-vicks9985.firebaseapp.com/main.rb",
{
"name": "admin",
"email": "admin@example.com",
"password": "secret",
"admin": true,
"role-value": 99,
}
), console.log("success");
{
"rules": {
".read": true,
"users": {
"$user": {
//can add a message if authenticated
".write": "auth.uid === $user"
}
},
"rooms": {
"$room": {
"users": {
// can write to the users list only if ADMINISTRATOR
"$user": {
"write":"newData.parent().child(auth.uid).val() === 99"
}
}
}
},
"messages": {
"$room": {
"$message": {
//can add a message if they are a MEMBER (if there was message/chat capability)
".write": "(!data.exists() && newData.exists() && root.child('rooms/' + $room + '/users/' + auth.uid).val() >= 10)"
}
}
}
}
}
$(document).ready(function() {
/**
*Set initial firebase ref. Use set to write in first admin user.
*/
var ref = new Firebase("https://starter-vicks9985.firebaseio.com/");
ref.set({
"name": "Admin",
"email": "admin@example.com",
"password": "secret",
"admin": true
});
/** Get email address from loginform, format email, get password
* Firebase keys cannot have a period (.) in them, so this converts the emails to valid keys
*/
var emailAddress = function emailToKey(emailAddress){
return btoa(emailAddress);
};
var password = document.getElementById('password');
/**
* Authorize user with email and password, passing in values from form.
*/
ref.authWithPassword({
email : emailAddress,
password : password,
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
return authData;
}
});
/**
* If user is logged in (valid), redirect to user profile
*/
ref.onAuth(function(authData) {
window.open="https://starter-vicks9985.firebaseio.com/userprofile/userprofile.html";
})
});
答案 0 :(得分:0)
就像@Kato所说,这是一个代码转储所以请考虑创建一个mcve。虽然,请先查看下面的评论。
在看了一下你的代码之后,我会看到一些错误,我会指出:
Firebase托管是一种服务所谓静态应用程序的产品,它只包含客户端解释的文件。 Firebase的服务器不会解释您上传的任何代码。因此,Firebase托管不适合托管您的Ruby-on-Rails应用程序。
引用Firebase hosting's documentation:
我们提供所有静态内容(html,js,图像等)
话虽如此,请查看$.post()
的jQuery文档。
查看我对您的代码的评论:
$.post/("https://starter-vicks9985.firebaseapp.com/main.rb",
// ^ What is this '/'?
{
"name": "admin",
"email": "admin@example.com",
"password": "secret",
"admin": true,
"role-value": 99,
}
), console.log("success");
// ^ You are closing the function call, 'console.log' falls outside of it.
应该是什么样子:
$.post("https://starter-vicks9985.firebaseapp.com/main.rb", {
"name": "admin",
"email": "admin@example.com",
"password": "secret",
"admin": true,
"role-value": 99,
}, function() {
console.log("success");
});
假设您在Uncaught SyntaxError: Unexpected token {
中修复data-structure.js:14
...
Uncaught Error: Firebase.authWithPassword failed: First argument must contain the key "email" with type "string"
。emailAddress
传递给.authWithPassword()
。你这样声明emailAddress()
:
var emailAddress = function emailToKey(emailAddress){
return btoa(emailAddress);
};
因此,传递以下email
参数emailAddress()
,而不是作为电子邮件地址的字符串。
ref.authWithPassword({
email : emailAddress,
password : password,
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
return authData;
}
});
app.js
)等待并回复表单的提交。
我也在你的页面上,看了你的源代码,发现了一些问题。
index.html
<section class="loginform cf">
<form id= "login" form name="login" form type= "submit" accept-charset="utf-8">
<!-- extra^space ^duplicate "form"^ ^again, space -->
...
</form>
</section>
data-structures.js
同样,您在此处遇到与上述相同的错误(&#39; /&#39;和关闭括号),但您传递post
的对象是格式不正确
$.post/("https://starter-vicks9985.firebaseapp.com/main.rb",
{
"users" //missing ':' after users
//the following objects do not have keys - should be {"users":{"someUserKey1":{...},"someUserKey2":{...}}} etc.
{
"name": "admin",
"email": "...",
"password": "...",
"admin": true,
"role-value": 99,
},
{
"name": "aaa",
...
},
{
"name": "bbb",
...
}
},
), console.log("success");
同样的事情也适用于&#34;群组的post
号码&#34;。
总之,我建议做更多的研究和阅读文档:)