Issue with empty displayName in Firebase when logging in using signInWithCredential
P粉328911308
2023-09-04 09:43:01
<p>I'm using Google Login and the user is prompted for their Google account email and password: </p>
<pre class="brush:php;toolbar:false;">const auth = getAuth(app);
const userCredentials = await signInWithCredential(
auth,
GoogleAuthProvider.credential(null, token)
);
console.log(userCredentials.user)</pre>
<p>I successfully obtained the user object, including <code>email</code>, <code>photoURL</code>, <code>uid</code> and other information, but <code> displayName</code> is empty. Is this a bug or expected result? I tried creating a new Gmail account, including name and nickname, but <code>displayName</code> was still empty. How do I get <code>displayName</code>? </p>
I found a way to "hack" the issue about not being able to get
displayName
fromsignInWithCredential
. Obviously, as said before,displayName
is always null. However, if you use thegoogle_sign_in
library to sign in and inspect theGoogleSignInAccount
object, you will see that you will have that display name. You can then log in to Firebase using the Google login credentials you used earlier. Here is the code I use:In the
user
variables you will have all the user related variables fromfirebase
authentication likeuid
and fromgoogleSignInAccount In
you can get the display name. Hope this helps someone!By default, the
displayName
attribute ofUser
is empty. It depends on whether the user setdisplayName
when creating or updating. But by default it isnull
.If you want to update
displayName
somewhere, you can use the following method:updateProfile
:If you are sure that you have
displayName
in your Google account, but it is not showing up inuser.displayName
, then you need to check what you passed toGoogleAuthProvider.credential( )
Is theidToken
of the methodnull
? Make sure you also pass a valididToken
.