From 3ffd326620eb17560b49e825afb5eca55c13098d Mon Sep 17 00:00:00 2001 From: Besmir Beqiri Date: Thu, 14 Mar 2024 12:59:01 +0100 Subject: [PATCH] Add `getEmail()` method to the to easily retrieve the email from the user's attributes after successful login --- .../jpro/platform/auth/core/authentication/User.java | 10 ++++++++++ .../auth/example/login/page/SignedInPage.java | 2 +- .../auth/example/oauth/page/SignedInUserPage.java | 11 ++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java b/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java index 4f5d7b94..e129934e 100755 --- a/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java +++ b/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java @@ -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"); + } } diff --git a/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/login/page/SignedInPage.java b/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/login/page/SignedInPage.java index 1aae534f..8a20fcbe 100644 --- a/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/login/page/SignedInPage.java +++ b/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/login/page/SignedInPage.java @@ -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); diff --git a/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/oauth/page/SignedInUserPage.java b/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/oauth/page/SignedInUserPage.java index 3a6e8069..17ea13e3 100644 --- a/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/oauth/page/SignedInUserPage.java +++ b/jpro-auth/example/src/main/java/one/jpro/platform/auth/example/oauth/page/SignedInUserPage.java @@ -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", @@ -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); @@ -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); @@ -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);