I'm trying to upload my user's files to a bucket connected to their uid. So, in order to do it I try to grab their uid from the following code in my upload.js file:
const uploader = document.getElementById("uploader");
const fileButton = document.getElementById("fileButton");
fileButton.addEventListener('change', function(e)
{
// Get file
var file = e.target.files[0];
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
console.log("state = definitely signed in");
firebase.storage().ref('users').child(user.uid + "designs").put(file);
} else {
// No user is signed in.
console.log("state = definitely signed out");
}
});
});
Even after the user is logged in and the it directs them to the new page I always get informed that they aren't signed in. Should I use information from the session cookie instead? Any help would be appreciated!
After a few days I realised that I wasn't calling the id token correlated with the user that is logged in. This solved it:
firebase.auth().onAuthStateChanged(function(user)
{
if (user)
{
// User is signed in.
console.log(user);
user.getIdToken().then(function(idToken)
{ // <------ Check this line
console.log(idToken); // It shows the Firebase token now
});
console.log(user.uid);
firebase.storage().ref('users').child(user.uid + "/designs").put(file);
}
else
{
// No user is signed in.
console.log("state = definitely signed out");
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.