Ad

User Profile Image Url Is Null Using Google Firebase Auth

I am using google firebase auth method to authenticate users I am fetching users profile info using firebase method and saving it on server. When I am trying to save users image uri on server it is showing null value.

Here is my code below:

FirebaseAuth fAuth = FirebaseAuth.getInstance();
FirebaseUser user = fAuth.getCurrentUser();

if(user != null){

     saveData();
}  

private void saveData(){

    final String uid = user.getUid();
    final String email = user.getEmail();
    final Uri profileImage  = user.getPhotoUrl();
    final String name = user.getDisplayName();

 RequestQueue requestQueue = Volley.newRequestQueue(BookForm.this);

 StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){

    @Override
        public void onResponse(String response) {

            if(response.equals("Successfully inserted")){

               Toast.makeText(getApplicationContext(),"Successful",Toast.LENGTH_SHORT.show();
            }
        }

  }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        NetworkResponse status = error.networkResponse;
            Toast.makeText(getApplicationContext(),"Error:"+status,Toast.LENGTH_SHORT).show();
        }
    }){

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            HashMap<String,String> map = new HashMap<>();

            map.put("userId",uid);
            map.put("email",email);
            map.put("name",name);
            map.put("profileImage", String.valueOf(profileImage));

            return map;
        }
    };

    requestQueue.add(stringRequest);
} 

This is how I am sending data to the server.

router.post('/addData', (req,res) => {

var data = {

    User_id: req.body.userId,
    Email:req.body.email,
    Profile_image:req.body.profileImage,
    Name: req.body.name
  };

  MongoClient.connect(dburl, {useNewUrlParser:true} ,(err,client) => {

          if(err){

            console.log("Error".red, +err);
          }
          else{

            var collect = client.db('my_db').collection('Example');

            collect.insertOne(data, (err,resp) =>{

                  if(err){

                    console.log("Error".red, +err);
                  }
                  else{

                    res.send("Successfully inserted");
                  }

                  client.close();
            });

        }

   });

});

Someone please let me know why image uri is showing null after saved to server. Any help would be appreciated.

THANKS

Ad

Answer

There is no requirement that the authentication provider must give you an image URL for the user. If the provider doesn't have one (maybe because the user never provided an image), then you will get null. Your code should be able to handle this case.

Note that the API documentation for getPhotoUrl() specifically says:

This field will be automatically populated on account creation if the AuthCredential used on signInWithCredential(AuthCredential) contained such information.

Ad
source: stackoverflow.com
Ad