-
Notifications
You must be signed in to change notification settings - Fork 145
Adding Java samples for listUsersAsync() and setCustomUserClaimsAsync() #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,10 @@ | |
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.auth.ExportedUserRecord; | ||
import com.google.firebase.auth.FirebaseAuth; | ||
import com.google.firebase.auth.FirebaseToken; | ||
import com.google.firebase.auth.ListUsersPage; | ||
import com.google.firebase.auth.UserRecord; | ||
import com.google.firebase.auth.UserRecord.CreateRequest; | ||
import com.google.firebase.auth.UserRecord.UpdateRequest; | ||
|
@@ -91,6 +93,81 @@ public static void updateUser(String uid) throws InterruptedException, Execution | |
// [END update_user] | ||
} | ||
|
||
public static void setCustomUserClaims( | ||
String uid) throws InterruptedException, ExecutionException { | ||
// [START set_custom_user_claims] | ||
// Set admin privilege on the user corresponding to uid. | ||
Map<String, Object> claims = new HashMap<>(); | ||
claims.put("admin", true); | ||
FirebaseAuth.getInstance().setCustomClaimsAsync(uid, claims).get(); | ||
// The new custom claims will propagate to the user's ID token the | ||
// next time a new one is issued. | ||
// [END set_custom_user_claims] | ||
|
||
String idToken = "id_token"; | ||
// [START verify_custom_claims] | ||
// Verify the ID token first. | ||
FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get(); | ||
if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) { | ||
// Allow access to requested admin resource. | ||
} | ||
// [END verify_custom_claims] | ||
|
||
// [START read_custom_user_claims] | ||
// Lookup the user associated with the specified uid. | ||
UserRecord user = FirebaseAuth.getInstance().getUserAsync(uid).get(); | ||
System.out.println(user.getCustomClaims().get("admin")); | ||
// [END read_custom_user_claims] | ||
} | ||
|
||
public static void setCustomUserClaimsScript() throws InterruptedException, ExecutionException { | ||
// [START set_custom_user_claims_script] | ||
UserRecord user = FirebaseAuth.getInstance() | ||
.getUserByEmailAsync("[email protected]").get(); | ||
// Confirm user is verified. | ||
if (user.isEmailVerified()) { | ||
Map<String, Object> claims = new HashMap<>(); | ||
claims.put("admin", true); | ||
FirebaseAuth.getInstance().setCustomClaimsAsync(user.getUid(), claims).get(); | ||
} | ||
// [END set_custom_user_claims_script] | ||
} | ||
|
||
public static void setCustomUserClaimsInc() throws InterruptedException, ExecutionException { | ||
// [START set_custom_user_claims_incremental] | ||
UserRecord user = FirebaseAuth.getInstance() | ||
.getUserByEmailAsync("[email protected]").get(); | ||
// Add incremental custom claim without overwriting the existing claims. | ||
Map<String, Object> currentClaims = user.getCustomClaims(); | ||
if (Boolean.TRUE.equals(currentClaims.get("admin"))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
// Add level. | ||
currentClaims.put("level", 10); | ||
// Add custom claims for additional privileges. | ||
FirebaseAuth.getInstance().setCustomClaimsAsync(user.getUid(), currentClaims).get(); | ||
} | ||
// [END set_custom_user_claims_incremental] | ||
} | ||
|
||
public static void listAllUsers() throws InterruptedException, ExecutionException { | ||
// [START list_all_users] | ||
// Start listing users from the beginning, 1000 at a time. | ||
ListUsersPage page = FirebaseAuth.getInstance().listUsersAsync(null).get(); | ||
while (page != null) { | ||
for (ExportedUserRecord user : page.getValues()) { | ||
System.out.println("User: " + user.getUid()); | ||
} | ||
page = page.getNextPage(); | ||
} | ||
|
||
// Iterate through all users. This will still retrieve users in batches, | ||
// buffering no more than 1000 users in memory at a time. | ||
page = FirebaseAuth.getInstance().listUsersAsync(null).get(); | ||
for (ExportedUserRecord user : page.iterateAll()) { | ||
System.out.println("User: " + user.getUid()); | ||
} | ||
// [END list_all_users] | ||
} | ||
|
||
public static void deleteUser(String uid) throws InterruptedException, ExecutionException { | ||
// [START delete_user] | ||
FirebaseAuth.getInstance().deleteUserAsync(uid).get(); | ||
|
@@ -155,6 +232,8 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc | |
getUserByEmail("[email protected]"); | ||
getUserByPhoneNumber("+11234567890"); | ||
updateUser("some-uid"); | ||
//setCustomUserClaims("some-uid"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this commented out on purpose ? |
||
listAllUsers(); | ||
deleteUser("some-uid"); | ||
createCustomToken(); | ||
createCustomTokenWithClaims(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean.TRUE.equals
seems a bit strange. Why not justif(decoded.getClaims().get("admin"))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getClaims()
returns aMap<String, Object>
. So when querying the map we need to deal with possible null values (if the key does not exist), and the returned value not being boolean.Boolean.TRUE.equals()
provides a one-liner that deals with both cases.