Skip to content

Commit

Permalink
Add getEmail() method to the to easily retrieve the email from the …
Browse files Browse the repository at this point in the history
…user's attributes after successful login
  • Loading branch information
besidev committed Mar 14, 2024
1 parent e61f609 commit 3ffd326
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,14 @@ private boolean hasKey(JSONObject json, String key) {
}
return exists;
}

/**
* Retrieve the user's email from the user's attributes.
*
* @return the email as a string
*/
public String getEmail() {
return toJSON().getJSONObject(User.KEY_ATTRIBUTES).getJSONObject("auth")
.getJSONObject("idToken").getJSONObject("payload").getString("email");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public SignedInPage(GoogleLoginApp app, OAuth2AuthenticationProvider authProvide
if (user == null) {
getChildren().add(headerLabel);
} else {
headerLabel.setText("Signed in as user: " + user.getName());
headerLabel.setText("Signed in as user: " + user.getName() + "\n(" + user.getEmail() + ")");

final var userInfoTextArea = new TextArea();
userInfoTextArea.setWrapText(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public SignedInUserPage(OAuthApp loginApp) {
if (authProvider == null) {
getChildren().add(headerLabel);
} else {
headerLabel.setText("Signed in user: " + (loginApp.getUserSession().getUser() == null ? ""
: loginApp.getUserSession().getUser().getName()));
final User user = loginApp.getUserSession().getUser();
if (user == null) {
headerLabel.setText("Not signed in.");
} else {
headerLabel.setText("Signed in user: " + user.getName() + "\n(" + user.getEmail() + ")");
}

final var authInfoBox = loginApp.createButtonWithDescription(
"Show authentication information about this user.", "Auth Info",
Expand Down Expand Up @@ -83,7 +87,6 @@ public SignedInUserPage(OAuthApp loginApp) {
final var revokeTokenBox = loginApp.createButtonWithDescription(
"Revoke the access token.", "Revoke Token",
event -> {
final var user = loginApp.getUserSession().getUser();
if (user == null) {
loginApp.setError(new IllegalStateException("User is not signed in."));
gotoPage(headerLabel, AUTH_ERROR_PATH);
Expand Down Expand Up @@ -113,7 +116,6 @@ public SignedInUserPage(OAuthApp loginApp) {
final var userInfoBox = loginApp.createButtonWithDescription(
"Get more user information from the provider.", "User Info",
event -> {
final var user = loginApp.getUserSession().getUser();
if (user == null) {
loginApp.setError(new IllegalStateException("User is not signed in."));
gotoPage(headerLabel, AUTH_ERROR_PATH);
Expand Down Expand Up @@ -145,7 +147,6 @@ public SignedInUserPage(OAuthApp loginApp) {
final var logoutBox = loginApp.createButtonWithDescription(
"Sign out from the provider.", "Sign Out",
event -> {
final var user = loginApp.getUserSession().getUser();
if (user == null) {
loginApp.setError(new IllegalStateException("User is not signed in."));
gotoPage(headerLabel, AUTH_ERROR_PATH);
Expand Down

0 comments on commit 3ffd326

Please sign in to comment.