简体   繁体   English

如何从Firebase获取当前用户的个人资料图片网址

[英]How to obtain the current user's profile image URL from Firebase

I've built a website where users can log in through Facebook, Google, or Twitter, and then their email, name, and profile picture URL will be saved to a Firebase database. 我建立了一个网站,用户可以通过Facebook,Google或Twitter登录,然后他们的电子邮件,姓名和个人资料图片网址将保存到Firebase数据库。 Now, I'm trying to obtain the currently logged in user's profile picture URL from the Firebase database, but I have no idea where to start. 现在,我正在尝试从Firebase数据库中获取当前登录用户的个人资料图片URL,但我不知道从哪里开始。 I've read Firebase's accessing data documentation, but didn't understand it enough to make it work. 我已经阅读过Firebase的访问数据文档,但对它的工作原理还不够了解。

This is the function that checks if the user is logged in and then checks if they're an admin. 这是检查用户是否已登录然后检查他们是否是管理员的功能。 I need to grab the currently logged in user's profile image on the line labeled "RIGHT HERE". 我需要在标有“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
        }
    });
});

Additionally, you can view my current Firebase app on https://mpacares.firebaseapp.com/ . 此外,您还可以在https://mpacares.firebaseapp.com/上查看我当前的Firebase应用。

Here's what worked for me in the end: 这是最终对我有用的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM