5

I am uploading images to firebase storage and would like to save their images as the photoURL in Firebase authentication. However when I go to upload the image URL after using get signed URL I receive the error

Error: The photoURL field must be a valid URL.

I know that the URL is valid as I have checked it from the console out put. I have tried using decodeURI and even gone so far as to look into the source code for the firebase-admin-node tracking all the way down to a file called auth-api-requests.ts which on line 252 checks the URL in a function named validator.isURL(request.photoUrl) this led me to the file where the function is defined validator.ts which defines .isURL() on line 152 in this function checks are performed against a string of forbidden characters. I dont want to tamper with the Firebase source code, but I am unable to find any solution. There should be an easier solution for the return from one google function .getSignedURL() to be used as a parameter in another .updateUser({photoURL:}) especially considering that one can no longer call on firebase.getDownloadURL() from the google cloud functions node. Thank you for any assistance you provide in solving this.

    var downloadURL = "";
   await admin.storage().bucket("gs://****firebase.appspot.com").file(storageRef).getSignedUrl({"action":"read","expires":Date.now() + 500*365*24*3600000}).then((value) => {
        console.log("value after requesting signed URL: " + JSON.stringify(value));
        downloadURL = value;
        return value;
    }).catch((error) => {
        console.log("error perfoming signed URL: " + error);
        return error;
    })


    const url = decodeURI(downloadURL)
    console.log("\nThe decodeURI url: " + url + "\n");

    await admin.auth().updateUser(userID,{photoURL:url}).then((user) => {
        console.log("User update ran a success: " + JSON.stringify(user));
        return true;
    }).catch((error) => {
        console.log("An error occured in getting the user: " + error);
        return error;
    });

2 Answers2

3

It is not a good idea to hard code the users photoURL (which is only populated for federated sign in users) as it may change. In other words, a Twitter user might change their profile photo. firebase.auth() provides you fresh user metadata upon user sign in.

It only adds maintenance overhead to save this type of metadata - there's no need to bother with this.

Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • Thank you for your response I never really thought about it as being mainly for federated data. You're right there is not much gained by attaching the photoURL to the users auth() profile that couldn't be achieved through adding another node in the firebase database. – TheRedCamaro3.0 3.0 Jan 11 '19 at 05:56
1

You should use the Authenticated URL which is definitely a valid URL, instead of the gsutil URI - gs://~

https://storage.cloud.google.com/${PROJECT_ID}.appspot.com/${pathToFile}
Chukwuma Nwaugha
  • 575
  • 8
  • 17