You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(user.controller): add new endpoints for managing claims and roles
Refactor the existing `setClaims` endpoint to `setRoleClaims` to distinguish between generic
claims and role-based claims. Introduce a new `setClaims` endpoint for setting generic claims.
Add corresponding get endpoints for retrieving both roles and claims, enhancing clarity in
API usage.
docs(README): update documentation with new role claims functionality
Update the README to reflect changes in API calls such as `setClaimsRoleBase` and details for
handling custom claims with `rolesClaimKey`. Clarify examples provided for role-based access
control to match the updated function signatures.
test(app-local-validation.e2e-spec.ts): enhance e2e tests with local environment variable support
Introduce `FIREBASE_TEST_USER_LOCAL` environment variable for improved test isolation.
Add additional test cases for new set and get claims functions to ensure comprehensive coverage.
fix(firebase.guard): refactor token verification and role handling logic
Improve token verification by abstracting logic into `verifyToken` for cleaner error handling.
Refactor role validation to a dedicated method `handleRoleValidation` to modularize complex
logic and improve maintainability.
miscellaneous:
- Add mock for custom claims to support testing scenarios.
- Update the FirebaseProvider to handle different keys for roles, supporting flexible claim
structures.
Copy file name to clipboardExpand all lines: README.md
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,7 @@ import { FirebaseAuthGuard } from '@alpha018/nestjs-firebase-auth';
55
55
checkRevoked: true, // Set to true if you want to check for revoked Firebase tokens
56
56
validateRole: true, // Set to true if you want to validate user roles
57
57
useLocalRoles: true, // Set to true if you want to validate user roles locally without firebase call
58
+
rolesClaimKey: 'user_roles'// Set the name of the key within the Firebase custom claims that stores user roles
58
59
},
59
60
},
60
61
}),
@@ -74,6 +75,7 @@ import { FirebaseAuthGuard } from '@alpha018/nestjs-firebase-auth';
74
75
|`auth.config.checkRevoked`|`boolean`| Optional | Set to `true` to check if the Firebase token has been revoked. Defaults to `false`. |
75
76
|`auth.config.validateRole`|`boolean`| Optional | Set to `true` to validate user roles using Firebase custom claims. Defaults to `false`. |
76
77
|`auth.config.useLocalRoles`|`boolean`| Optional | Set to `true` to validate user roles using local custom claims inside the JWT token. Defaults to `false`. **Note:** If you update the claims, previously issued tokens may still contain outdated roles and remain valid. |
78
+
|`auth.config.rolesClaimKey`|`string`| Optional | The name of the key within the Firebase custom claims that stores user roles. Defaults to `'roles'`. This allows you to customize the property name for roles in your custom claims object. |
77
79
78
80
79
81
### Auth Guard Without Role Validation
@@ -96,7 +98,7 @@ export class AppController {
96
98
97
99
### Auth Guard With Role Validation
98
100
99
-
To enforce role-based access control, you need to set custom claims in Firebase. Here's how you can set custom claims:
101
+
To enforce role-based access control, you need to set role-based custom claims in Firebase. Here's how you can set roles for a user using `setClaimsRoleBase`:
'some-firebase-uid', // The UID of the user you want to set roles for
120
+
[Roles.ADMIN]
119
121
);
120
122
return { status: 'ok' }
121
123
}
@@ -213,7 +215,7 @@ export class AppController {
213
215
> **Note:** Starting from version `>=1.7.x`, these two decorators are explicitly separated to avoid confusion (see [issue #11](https://github.com/Alpha018/nestjs-firebase-auth/issues/11)):
214
216
215
217
-`@FirebaseUser()` → Returns the **full decoded token** (`auth.DecodedIdToken`).
216
-
-`@FirebaseUserClaims()` → Returns only the **custom claims** (roles/permissions) defined for the user.
218
+
-`@FirebaseUserClaims()` → Returns only the **custom role claims** (roles/permissions) defined for the user.
217
219
218
220
This separation ensures that developers can access both the raw Firebase user object and the role/claims information independently.
0 commit comments