I am integrating Google Sign In into my flutter app using the google_sign_in package, and I notice that once the user logs in, you are able to see a display name, but not a first and last name:
final GoogleSignInAccount googleSignInAccount =
await googleSignIn.signIn();
final name = googleSignInAccount.displayName; // exists
final firstName = googleSignInAccount.givenName; // does not exist
final lastName = googleSignInAccount.familyName; // does not exist
I also noticed that the Android and iOS libraries for Google Sign In do include the givenName
and familyName
fields, but for some reason it is left out of the flutter library.
Am I missing something? Is there a way to retrieve these fields using an additional api call?
Note: Please do not respond saying to just split the display name into parts via googleSignInAccount.displayName.split(" ");
. This is not a solution since the display name is not always the user's full name, or the display name may be more than 2 words.
I found that these values can be extracted from the id token retrieved in the authentication step below:
final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
final idToken = googleSignInAuthentication.idToken;
Create a method for parsing the JWT token into a Map:
static Map<String, dynamic> parseJwt(String token) {
// validate token
if (token == null) return null;
final List<String> parts = token.split('.');
if (parts.length != 3) {
return null;
}
// retrieve token payload
final String payload = parts[1];
final String normalized = base64Url.normalize(payload);
final String resp = utf8.decode(base64Url.decode(normalized));
// convert to Map
final payloadMap = json.decode(resp);
if (payloadMap is! Map<String, dynamic>) {
return null;
}
return payloadMap;
}
From here, the given_name
and family_name
are easily retrievable:
Map<String, dynamic> idMap = parseJwt(idToken);
final String firstName = idMap["given_name"];
final String lastName = idMap["family_name"];