首页 文章

如何从Firebase获取当前用户的 Profiles 图片网址

提问于
浏览
3

我 Build 了一个网站,用户可以通过Facebook,Google或Twitter登录,然后他们的电子邮件,姓名和 Profiles 图片网址将保存到Firebase数据库 . 现在,我正在尝试从Firebase数据库中获取当前登录用户的 Profiles 图片URL,但我不知道从哪里开始 . 我已经阅读过Firebase的访问数据文档,但对它的工作原理还不够了解 .

这是检查用户是否已登录然后检查他们是否为管理员的功能 . 我需要在标有“RIGHT HERE”的行上抓取当前登录用户的 Profiles 图片 .

$(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应用 .

1 回答

  • 2

    这到底对我有用:

    $(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);
            });
    

相关问题