Firebase用户上传和个人资料图片 [英] Firebase User Uploads and Profile Pictures
问题描述
您好,需要帮助
我目前有一些JavaScript代码,允许登录用户将图像上传到Firebase存储.我希望他们上传的图像成为他们的个人资料照片,但是我似乎无法将上传的图像链接回特定用户?
I currently have some JavaScript code that allows a signed in user to upload an image to Firebase storage. I want the image that they upload to be their profile picture, however I don't seem to be able to link the uploaded image back to the specific user?
它可以正常工作,我可以在控制台中看到上传的图像,但是无法识别出什么用户上传了该图像.
It works, I can see the uploaded image in the console, but nothing to identify what user uploaded it.
//Upload Profile Picture
//Altered code from: Firebase Youtube Channel.
//Get Elements
var uploader = document.getElementById('uploader');
var fileButton = document.getElementById('fileButton');
//Listen for file
fileButton.addEventListener('change', function(e){
//Get File
var file = e.target.files[0];
//Create a Storage Ref
var storageRef = firebase.storage().ref('profilePictures/' + file.name);
//Upload file
var task = storageRef.put(file);
var user = firebase.auth().currentUser;
//Update Progress Bar
task.on('state_changed',
function progress(snapshot){
var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) *100;
uploader.value = percentage;
//if percentage = 100
//$(".overlay").hide();
},
function error(err){
},
function complete(){
}
);
});
//Display Profile Picture
function showUserDetails(){
var user = firebase.auth().currentUser;
var name, photoUrl;
if (user != null) {
name = user.displayName;
photoUrl = user.photoURL;
document.getElementById('dp').innerHTML=photoURL;
document.getElementById('username').innerHTML=name;
}}
我在Firebase文档中看到了下面的代码,但是我不知道如何更新照片URL图像或显示它.
I saw the code below in the Firebase documentation, but I don't understand how to update the photo URL image or display it.
var user = firebase.auth().currentUser;
var name, email, photoUrl, uid;
if (user != null) {
name = user.displayName;
email = user.email;
photoUrl = user.photoURL;
uid = user.uid; // The user's ID, unique to the Firebase project. Do NOT use
// this value to authenticate with your backend server, if
// you have one. Use User.getToken() instead.
}
在此先感谢您的帮助或建议.
Many thanks in advance for any help or advice.
推荐答案
您将要按用户名存储文件:
You'll want to store the files by username:
// Get current username
var user = firebase.auth().currentUser;
// Create a Storage Ref w/ username
var storageRef = firebase.storage().ref(user + '/profilePicture/' + file.name);
// Upload file
var task = storageRef.put(file);
因此,如果用户是user:12345
,文件名是profile.jpg
,则文件将另存为user:12345/profilePicture/profile.jpg
.您可以使用安全规则对此进行保护,如下所示:
So if the user is user:12345
and the file name is profile.jpg
, then the file will be saved as user:12345/profilePicture/profile.jpg
. You can protect this with Security Rules as follows:
service firebase.storage {
match /b/<your-firebase-storage-bucket>/o {
match /{userId}/profilePicture/{fileName} {
// Anyone can read
allow read;
// Only the user can upload their own profile picture
// Profile picture must be of content-type "image/*"
allow write: if request.auth.uid == userId
&& request.resource.contentType.matches('image/.+');
}
}
}
这篇关于Firebase用户上传和个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!