Skip to content

Commit f0d6f49

Browse files
authored
Fix a small bug in setCustomUserClaims() auth snippet (#747)
* fix a small bug in setCustomUserClaims() auth snippet * Revert changes in setCustomUserClaims and create new function setCustomUserClaimsTenant
1 parent 6145491 commit f0d6f49

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/test/java/com/google/firebase/snippets/FirebaseAuthSnippets.java

+27
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,33 @@ public void customClaimsVerifyTenant(
11501150
// [END verify_custom_claims_tenant]
11511151
}
11521152

1153+
public static void setCustomUserClaimsTenant(TenantAwareFirebaseAuth tenantAuth,
1154+
String uid) throws FirebaseAuthException {
1155+
// [START set_custom_user_claims_tenant]
1156+
// Set admin privilege on the user corresponding to uid in a specific tenant.
1157+
Map<String, Object> claims = new HashMap<>();
1158+
claims.put("admin", true);
1159+
tenantAuth.setCustomUserClaims(uid, claims);
1160+
// The new custom claims will propagate to the user's ID token the
1161+
// next time a new one is issued.
1162+
// [END set_custom_user_claims_tenant]
1163+
1164+
String idToken = "id_token";
1165+
// [START verify_custom_claims_tenant]
1166+
// Verify the ID token first.
1167+
FirebaseToken decoded = tenantAuth.verifyIdToken(idToken);
1168+
if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
1169+
// Allow access to requested admin resource.
1170+
}
1171+
// [END verify_custom_claims_tenant]
1172+
1173+
// [START read_custom_user_claims_tenant]
1174+
// Lookup the user associated with the specified uid in a specific tenant.
1175+
UserRecord user = tenantAuth.getUser(uid);
1176+
System.out.println(user.getCustomClaims().get("admin"));
1177+
// [END read_custom_user_claims_tenant]
1178+
}
1179+
11531180
public void generateEmailVerificationLinkTenant(
11541181
TenantAwareFirebaseAuth tenantAuth,
11551182
String email,

0 commit comments

Comments
 (0)