【问题标题】:How to obtain the current user's profile image URL from Firebase如何从 Firebase 获取当前用户的个人资料图片 URL
【发布时间】:2016-04-29 15:44:24
【问题描述】:

我建立了一个网站,用户可以通过 Facebook、Google 或 Twitter 登录,然后他们的电子邮件、姓名和个人资料图片 URL 将保存到 Firebase 数据库。现在,我试图从 Firebase 数据库中获取当前登录用户的个人资料图片 URL,但我不知道从哪里开始。我已阅读 Firebase 的访问数据文档,但对它的理解不足以使其正常工作。

这是检查用户是否登录然后检查他们是否是管理员的功能。我需要在标有“RIGHT HERE”的行中获取当前登录用户的个人资料图像。

$(document).ready(function() { 
    var ref = new Firebase("https://mpacares.firebaseio.com/");
    ref.onAuth(function (auth) {
        if (auth) {
            var userRef = ref.child('users').child(auth.uid);
            userRef.on('value', function (snap) {
                var user = snap.val(); 
                if (user) {
                    // RIGHT HERE: set the user image src to user.picture
                } else {
                    // TODO: hide the user image
                }
            }, function (error) {
                console.log(error);
            });

            var adminRef = ref.child('admins').child(auth.uid);
            adminRef.on('value', function (snap) {
                var user = snap.val(); 
                if (user) {
                    console.log("You're an admin!");
                    // enable admin button
                } else {
                    console.log("Sorry, no access for you.");
                    // disable admin button
                }
            }, function (error) {
                console.log(error);
            });
        } else {
            // logged out
        }
    });
});

此外,您可以在https://mpacares.firebaseapp.com/ 上查看我当前的 Firebase 应用。

【问题讨论】:

  • 你有没有得到这个问题的答案?目前在成功验证后检索 Google+ 个人资料照片时遇到问题。
  • 是的,我添加了一个答案。

标签: javascript jquery database firebase


【解决方案1】:

这就是最终对我有用的方法:

$(document).ready(function() { 
var ref = new Firebase("https://mpacares.firebaseio.com/");
ref.onAuth(function (auth) {
    if (auth) {
        var userRef = ref.child('users').child(auth.uid);
        userRef.on('value', function (snap) {
            var user = snap.val(); 
            if (user) {
                $(document).ready(function() { 
                    var ref = new Firebase("https://mpacares.firebaseio.com/");
                    var user = ref.getAuth(); 
                    console.log(user); 
                    var userRef = ref.child('users').child(user.uid); 
                    userRef.once("value", function(snap) { 
                        var user = snap.val(); 
                        console.log(user); 
                        console.log(user.name);
                        console.log(user.picture); 
                        console.log(user.email); 
                        var userName = user.name; 
                        var userPicURL = user.picture; 
                        var userEmail = user.email; 
                        document.getElementById("account-txt").innerHTML = user.name; 
                        $(".account-img").attr("src", userPicURL); 
                    });  
                });
            } else {
                $(".account-txt").hide(); 
                $(".account-img").hide(); 
            }
        }, function (error) {
            console.log(error);
        });

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 2021-04-04
    • 2019-11-04
    • 1970-01-01
    • 2014-09-15
    • 2017-12-20
    相关资源
    最近更新 更多