-
-
Notifications
You must be signed in to change notification settings - Fork 2
Decorators
Protects a route or controller using the FirebaseGuard. It ensures that the request contains a valid Firebase ID token in the Authorization: Bearer <token> header, and can compose roles, claims, and policies on the same route via @Auth({ roles, claims, policies }).
Usage:
@Auth()
@Get('secure')
getSecureData() { ... }Extracts the decoded ID token from the request object. This provides access to user information like uid, email, email_verified, and any custom claims.
Important
This decorator requires the route to be protected by @Auth() (or FirebaseGuard) to function. Without the guard, the user object is not attached to the request.
Usage:
@Injectable()
@Controller('user')
export class UserController {
@Get('me')
getMe(@FirebaseUser() user: DecodedIdToken) {
return {
uid: user.uid,
email: user.email,
};
}
}Specifies the roles required to access a route. The user must have at least one of the specified roles to be granted access.
Requirements:
-
validateRole: truemust be set inFirebaseAdminModuleconfiguration. - The user must have a custom claim (default key:
'roles') containing an array of roles.
Usage:
@Auth()
@Roles('ADMIN', 'SUPERUSER')
@Get('admin-panel')
getAdminPanel() { ... }Extracts the roles claim array from the authenticated user's token or custom claims. This is useful if you want to inspect the user's roles within your controller logic.
Usage:
@Get('my-roles')
getMyRoles(@FirebaseRolesClaims() roles: string[]) {
return roles;
}Specifies the fine-grained claims required to access a route. The user must have every one of the specified claims, unlike @Roles(), which is satisfied by any one. Applies FirebaseGuard and ClaimsGuard.
Requirements:
- The user must have a custom claim (default key:
'permissions', configurable viaclaimsClaimKey) containing an array of claims.
Usage:
enum UsersClaim {
READ = 'users:read',
WRITE = 'users:write',
}
@RequireClaims(UsersClaim.READ, UsersClaim.WRITE)
@Get(':id')
getUser(@Param('id') id: string) { ... }Note
@Auth({ claims: [...] }) is an equivalent way to set the same claims; use whichever reads better at the call site, but not both on the same route. See Claims (Fine-Grained) for the full mechanism.
Specifies the policies (ABAC) required to access a route. Applies FirebaseGuard and PoliciesGuard, and every listed policy must resolve for the request to proceed. Works exactly like @UseGuards(): each argument is either a PolicyHandler class (resolved through Nest's DI container) or an instance built with new. See Authorization-Policies for how to define one.
Usage:
@Policies(ResourceOwnerPolicyHandler)
@Get(':ownerId')
getResource(@Param('ownerId') ownerId: string) { ... }Note
@Auth({ policies: [...] }) is an equivalent way to set the same policies; use whichever reads better at the call site, but not both on the same route.
GitHub Repository | Issues | Releases
If you find this project useful, you can support it by buying me a coffee ☕️. If you can't contribute financially, a ⭐ on the repo is also greatly appreciated!
MIT License © 2024 Tomás Alegre