Skip to content

Fix a small bug in setCustomUserClaims() auth snippet #747

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 2 commits into from
Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,33 @@ public void customClaimsVerifyTenant(
// [END verify_custom_claims_tenant]
}

public static void setCustomUserClaimsTenant(TenantAwareFirebaseAuth tenantAuth,
String uid) throws FirebaseAuthException {
// [START set_custom_user_claims_tenant]
// Set admin privilege on the user corresponding to uid in a specific tenant.
Map<String, Object> claims = new HashMap<>();
claims.put("admin", true);
tenantAuth.setCustomUserClaims(uid, claims);
// 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_tenant]

String idToken = "id_token";
// [START verify_custom_claims_tenant]
// Verify the ID token first.
FirebaseToken decoded = tenantAuth.verifyIdToken(idToken);
if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
// Allow access to requested admin resource.
}
// [END verify_custom_claims_tenant]

// [START read_custom_user_claims_tenant]
// Lookup the user associated with the specified uid in a specific tenant.
UserRecord user = tenantAuth.getUser(uid);
System.out.println(user.getCustomClaims().get("admin"));
// [END read_custom_user_claims_tenant]
}

public void generateEmailVerificationLinkTenant(
TenantAwareFirebaseAuth tenantAuth,
String email,
Expand Down