douxiong3245 2018-10-19 23:00
浏览 85
已采纳

每次注册时,如何将用户和其他详细信息添加到数据库中的用户节点?

I've tried to create the code by using what I've found online about adding additional details to a user node, but I've come up without a clear cut and definite solution. I can't make heads or tails with Google searches and other such things, but I only found a depreciated solution that's not supported and does not work with the current version of Firebase. So far, here's what I've come up with:

(function(){
  var ref = new firebase.database();
  ref.onAuth(function(authData) {
    if (authData && isNewUser) {
      // save the user's profile into Firebase so we can list users,
      // use them in Security and Firebase Rules, and show profiles
      ref.child("users").child(authData.uid).set({
        provider: authData.provider,
        name: getName(authData)
      });
    }
  });
    var ui = new firebaseui.auth.AuthUI(firebase.auth());
    var uiConfig = {
        callbacks: {
          signInSuccessWithAuthResult: function(authResult, redirectUrl) {
            // User successfully signed in.
            // Return type determines whether we continue the redirect automatically
            // or whether we leave that to developer to handle.
            return true;
          },
          uiShown: function() {
            // The widget is rendered.
            // Hide the loader.
            document.getElementById('loader').style.display = 'none';
          }
        },
        // Will use popup for IDP Providers sign-in flow instead of the default, redirect.
        signInFlow: 'popup',
        signInSuccessUrl: 'map_menu.php',
        signInOptions: [
          firebase.auth.EmailAuthProvider.PROVIDER_ID,
        ],
        // Terms of service url.
        tosUrl: 'map_menu.php',
        // Privacy policy url.
        privacyPolicyUrl: 'map_menu.php'
      };

      ui.start('#firebaseui-auth-container', uiConfig);
})()
  • 写回答

1条回答 默认 最新

  • dousao9569 2018-10-20 06:07
    关注

    You should add a code to get the user data like uid as the main child of the user and all other details as sub child nodes of this node in your database.

    A Json structure that looks something like this:

    {
          "accounts" : {
            "userId value" : {
              "email" : "",
              "userId" : ""
            },
            "userId value" : {
              "email" : "",
              "userId" : ""
            }
          }
    

    To save the users' details in this node, just when they are registered, you can add a code like this:

    var uid = firebase.auth().currentUser.uid;
    firebase.database().ref().child('accounts').child(uid).set({
      email: user.email,
      userId: uid
    })

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?