首页 > 解决方案 > 如何将用户设置为登录 firebase.auth(node.js)?

问题描述

我有一个使用自定义令牌登录的应用程序,写在 webpack 观察上。我现在要做的是在通过自定义令牌成功登录后将用户标记为登录 firebase auth 和 firebase firestore,我在其中收集了用户,并为每个用户记录了数据和一些 uid。我不知道该怎么做。

这是我的代码:

generateToken(uid) {   
const uid = 'some-uid';
this.trigger(this.signals.action.onGenerateToken);
firebase.admin.auth().createCustomToken(uid)
.then((customToken) => {
  console.log(customToken);
})
.catch(function (error){
  if (error.Code === 'auth/invalid-custom-token') {
    alert('The token you provided is not valid.');
  }
  else {
  this.trigger(this.signals.error.onGenerateToken);
  }
})
 }

login(uid) {
firebase.auth().signInWithCustomToken(token)
.then(function() {
  var user = firebase.auth().currentUser; 
  if (user) {
    //mark the user as active (logged) after successful login on firebase auth and firebase firestore 
   };
  this.trigger(this.signals.success.onLogin);
})
.catch(function(error) {
  if (errorCode === 'auth/too-many-requests') {
    this.trigger(this.signals.error.tooManyRequests);
  }
  else {
      this.trigger(this.signals.error.userDisabled);
  }
  });
  }

标签: javascriptnode.jsfirebase

解决方案


如果我正确理解您的问题,请首先创建对您的用户文档的引用,然后调用update()该引用并传入一个包含您要更新的属性及其新值的对象。

let userRef = firebase.database().ref('users/' + userId);
userRef.update({active:true});

查看firebase 文档以获取有关如何读取和写入 firebase 的更多信息。


推荐阅读