首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularFire简单登录

AngularFire简单登录
EN

Stack Overflow用户
提问于 2014-07-04 13:31:26
回答 1查看 418关注 0票数 2

我有一个带有用户存储的FireBase数据库。我也使用简单的登录电子邮件/。在用户商店中,我保存了用户的一些额外信息-例如,最后登录日期。这是我的工作流程--从注册到登录:

我注册一个用户;注册时它被添加到简单登录电子邮件/pw sote中;我还在用户存储中添加了注册用户(包括从simplelogin返回的id )。它存储在Firebase生成的唯一密钥下。

我以新用户的身份登录,当成功时,我从simplelogin商店获得一个user对象:

代码语言:javascript
复制
email-"testuser1@test.com"
firebaseAuthToken-"eyJ0eXAiOiJKV1QiLCJhbGci...SoXkddR3A88vAkENCy5ilIk"
id-"46"
isTemporaryPassword-false
md5_hash-"6a4b6cb2045fd55f706eaebd6ab5d4f7"
provider-"password"
uid-"simplelogin:46"

现在,我想更新用户存储中的相应用户--例如,将lastlogin键设置为now。但是,只有当我知道Firebase生成的密钥在下面时,我才能更新该用户。我怎么才能拿到那把钥匙?

识别用户存储中用户的唯一其他方法是检索用户存储中的所有用户,遍历所有用户并检查:当前id键值是否与登录用户的id键值匹配。在我看来有点笨拙,但我担心这是我唯一能用火柴进行查找的方法?

EN

回答 1

Stack Overflow用户

发布于 2014-07-04 21:59:55

当您保存注册用户时,您应该通过他们的uid而不是生成的id来保存他们。这样,当用户重新登录时,我们将使用uid从用户节点获取用户。

代码语言:javascript
复制
var fbRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com');
var auth = new FirebaseSimpleLogin(fbRef, function(error, user) {
  if (error) {
    console.error(error);
  } else if (user) {
    // when a user logs in we can update their lastLogin here
    // set the key to the uid for the user
    // this would look like: https://myapp.firebaseio.com/users/1
    fbRef.child('users').child(user.uid).update({
       lastLogin: Firebase.ServerValue.TIMESTAMP // the time they logged in
    });
  }
});

// here when we create a user we will set the key to the uid under the users node
auth.createUser(email, password, function(error, user) {
  // if there is no error
  if (!error) {
    // go to the users node, then set a location at the user's uid
    // this would look like: https://myapp.firebaseio.com/users/1
    fbRef.child('users').child(user.uid).set(user);
  }
});

创建用户时,我们的用户节点将如下所示:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24575517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档