Skip to content

Commit 123c98a

Browse files
authored
Merge pull request #41 from firebase/add-snippets
adding two custom claims snippets
2 parents 6f400dd + 505da33 commit 123c98a

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

auth/custom_claims.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,36 @@ admin.auth().getUser(uid).then((userRecord) => {
2929
// The claims can be accessed on the user record.
3030
console.log(userRecord.customClaims.admin);
3131
});
32-
// [END read_custom_user_claims]
32+
// [END read_custom_user_claims]
33+
34+
// [START set_custom_user_claims_script]
35+
admin.auth().getUserByEmail('[email protected]').then((user) => {
36+
// Confirm user is verified.
37+
if (user.emailVerified) {
38+
// Add custom claims for additional privileges.
39+
// This will be picked up by the user on token refresh or next sign in on new device.
40+
return admin.auth().setCustomUserClaims(user.uid, {
41+
admin: true
42+
});
43+
}
44+
})
45+
.catch((error) => {
46+
console.log(error);
47+
});
48+
// [END set_custom_user_claims_script]
49+
50+
// [START set_custom_user_claims_incremental]
51+
admin.auth().getUserByEmail('[email protected]').then((user) => {
52+
// Add incremental custom claim without overwriting existing claims.
53+
const currentCustomClaims = user.customClaims;
54+
if (currentCustomClaims.admin) {
55+
// Add level.
56+
currentCustomClaims['accessLevel'] = 10;
57+
// Add custom claims for additional privileges.
58+
return admin.auth().setCustomUserClaims(user.uid, currentCustomClaims);
59+
}
60+
})
61+
.catch((error) => {
62+
console.log(error);
63+
});
64+
// [END set_custom_user_claims_incremental]

0 commit comments

Comments
 (0)