VINNUSAURUS
VINNUSAURUS

Reputation: 1558

Google Cloud Functions is sending wrong Authentication values from app

I have a android app where i have updated the User Profile with new DisplayName but when calling a cloud function, the cloud function has wrong name.

Profile Update Code:

val profileUpdates = UserProfileChangeRequest.Builder()
                        .setDisplayName("Vinay")
                        .setPhotoUri(it)
                        .build()

FirebaseAuth.getInstance().currentUser!!.updateProfile(profileUpdates)
                        .addOnCompleteListener { task ->
                            if (task.isSuccessful) {
                                Log.d("post", "Updated Profile")
                                //Here Code Runs Successfully
                                // then call a cloud functio
                            }
                        }

but in cloud functions the userName: context.auth.token.name gives the actual provider displayname (VINNUSAURUS) and not the displayname I have changed before calling any cloud function.

Upvotes: 0

Views: 34

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317712

context.auth.token contains information about the JWT used with Firebase Authentication, provided by the client. It's the same as what would be available in Firebase security rules. This doesn't contain the user's display name.

The display name is stored by Firebase Auth, and you can get it in a Cloud Function execution with the Firebase Admin SDK. First you will need the UID of the user making the request. The UID is available in context.auth.uid, but only for callable and Realtime Database type triggers. If you are writing some other kind of trigger, you will need to figure out some other way to get the UID.

The API call provided by the Admin SDK to get the display name is getUser(). It returns a UserRecord object with the display name. You can also read more about it in the documentation.

Upvotes: 1

Related Questions