Artur
New Coder
Good afternoon, I can’t figure out how to upload a photo (avatar) to firebase, but so that the avatar for each user is different and saved at each user login. User registration via firebase Authentication
Code:
const [user, loading] = useAuthState(auth);
const [image , setImage] = useState("");
const storage = getStorage();
//upload function
const upload = ()=>{
const storageRef = ref(storage, `usersAvatar/${image.name}`);
if(image == null) return;
uploadBytes(storageRef, image).then(() =>{
getDownloadURL(storageRef).then((url) => {
setUrl(url);
})
})
}
// return
<input type="file" onChange={(e)=>{setImage(e.target.files[0])}}/>
<button onClick={upload}>Upload</button>