Javascript Firebase&;主干:将uid用作密钥时出现问题
我使用Firebase和TodoMVC为每个用户创建了登录名和唯一的todo列表,作为另一个项目的概念证明。我使用Firebase和Google登录用户,当一切正常时,他们会得到一个唯一的持久待办事项列表 当用户已经通过他们的浏览器登录到谷歌时,一切都会正常工作 问题发生在他们不在的时候。他们看到的是未定义用户的todo列表,而不是他们的todo列表,或者用户id下的空白列表,直到他们点击“刷新”按钮,然后一切又恢复正常。Firebase url在点击“刷新”后才会看到其uid。如果你登录到谷歌,你可以通过打开一个匿名窗口来复制错误 您可以在上看到我的代码中的错误,在上看到我的回购协议中的错误 这是我的身份验证码:Javascript Firebase&;主干:将uid用作密钥时出现问题,javascript,backbone.js,firebase,backbonefire,Javascript,Backbone.js,Firebase,Backbonefire,我使用Firebase和TodoMVC为每个用户创建了登录名和唯一的todo列表,作为另一个项目的概念证明。我使用Firebase和Google登录用户,当一切正常时,他们会得到一个唯一的持久待办事项列表 当用户已经通过他们的浏览器登录到谷歌时,一切都会正常工作 问题发生在他们不在的时候。他们看到的是未定义用户的todo列表,而不是他们的todo列表,或者用户id下的空白列表,直到他们点击“刷新”按钮,然后一切又恢复正常。Firebase url在点击“刷新”后才会看到其uid。如果你登录到谷歌
// Authenticate with Google
var ref = new Firebase(<firebase url>);
ref.onAuth(function(authData) {
if (authData) {
console.log("Authenticated successfully");
} else {
// Try to authenticate with Google via OAuth redirection
ref.authWithOAuthRedirect("google", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
}
});
}
})
// Create a callback which logs the current auth state
function authDataCallback(authData) {
if (authData) {
console.log("User " + authData.uid + " is logged in with " + authData.provider);
uid = authData.uid;
} else {
console.log("User is logged out");
}
}
//通过谷歌认证
var ref=新的Firebase();
参考onAuth(函数(authData){
if(authData){
console.log(“已成功验证”);
}否则{
//尝试通过OAuth重定向与Google进行身份验证
ref.authWithOAuthRedirect(“谷歌”,函数(错误,authData){
如果(错误){
log(“登录失败!”,错误);
}
});
}
})
//创建一个记录当前身份验证状态的回调
函数authDataCallback(authData){
if(authData){
console.log(“用户”+authData.uid+”使用“+authData.provider”登录);
uid=authData.uid;
}否则{
console.log(“用户已注销”);
}
}
这是获取用作firebase密钥的UID的代码:
// Get the uid of the user so we can save data on a per user basis
var ref = new Firebase(<firebase url>);
var authData = ref.getAuth();
if (authData) {
var uid = authData.uid;
console.log(uid);
}
// The collection of todos is backed by firebase instead of localstorage
var TodoList = Backbone.Firebase.Collection.extend({
// Reference to this collection's model.
model: app.Todo,
// Save all of the todos to firebase
url: <firebase url> + uid,
//获取用户的uid,以便我们可以按每个用户保存数据
var ref=新的Firebase();
var authData=ref.getAuth();
if(authData){
var uid=authData.uid;
控制台日志(uid);
}
//TODO集合由firebase而不是localstorage支持
var TodoList=Backbone.Firebase.Collection.extend({
//引用此集合的模型。
型号:app.Todo,
//将所有待办事项保存到firebase
url:+uid,
提前感谢您提供的任何建议!在用户通过身份验证之前,您正在调用
.getAuth()
// js/app.js
var app = app || {};
var ENTER_KEY = 13;
$(function() {
var ref = new Firebase(<firebase url>);
var authData = ref.getAuth();
if (authData) {
ref.authWithOAuthRedirect("google", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
// kick off app
new app.AppView();
}
});
} else {
new app.AppView();
}
});
你的应用程序严重依赖于uid
才能正常工作。因此,在你的情况下,一旦用户成功通过身份验证,你就需要启动应用程序的主干部分
// js/app.js
var app = app || {};
var ENTER_KEY = 13;
$(function() {
var ref = new Firebase(<firebase url>);
var authData = ref.getAuth();
if (authData) {
ref.authWithOAuthRedirect("google", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
// kick off app
new app.AppView();
}
});
} else {
new app.AppView();
}
});
您可以修改app.js,使其仅在用户经过身份验证后启动
// js/app.js
var app = app || {};
var ENTER_KEY = 13;
$(function() {
var ref = new Firebase(<firebase url>);
var authData = ref.getAuth();
if (authData) {
ref.authWithOAuthRedirect("google", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
// kick off app
new app.AppView();
}
});
} else {
new app.AppView();
}
});
//js/app.js
var-app=app |{};
var ENTER_KEY=13;
$(函数(){
var ref=新的Firebase();
var authData=ref.getAuth();
if(authData){
ref.authWithOAuthRedirect(“谷歌”,函数(错误,authData){
如果(错误){
log(“登录失败!”,错误);
}否则{
//启动应用程序
新建app.AppView();
}
});
}否则{
新建app.AppView();
}
});
虽然这会起作用,但它不是理想的解决方案。但是没有其他选项,因为您没有登录屏幕
理想情况下,您希望为用户提供一个登录位置,然后可以访问.getAuth()
值
另外,不要担心将
uid
存储在窗口上
是缓存的用户,因此没有网络调用来获取数据。你能创建一个Plnkr或JSBin来重现这个问题吗?它不一定是完整的应用程序,但只是一个显示你问题的示例。你更新了github页面网站吗?我仍然看到相同的代码。啊,我知道发生了什么。我已经更新了我对simp的回答取消登录过程。登录的回调在onAuth
的异步特性中丢失。在您的情况下,您只需要使用登录回调。如果您想收听onAuth
,您应该在AppView
中进行。啊,我一直忘了您没有登录按钮。这才是真正的解决方案。您应该加载页面时不要抛出重定向。这会混淆用户,并增加代码管理的复杂性,但我的更新答案现在应该可以帮助您。David--看起来我需要添加登录按钮。我是否还需要将其连接到.getAuth()代码?