Skip to content

Commit cdabd2f

Browse files
authored
Merge pull request #14 from Alpha018/fix/problem-decorator
problem decorator are explicitly separated to avoid confusion
2 parents b325ebd + 9bcca5e commit cdabd2f

File tree

8 files changed

+31
-23
lines changed

8 files changed

+31
-23
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ export class AppController {
207207
}
208208
}
209209
```
210+
211+
#### Difference Between `@FirebaseUser` and `@FirebaseUserClaims`
212+
213+
> **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+
215+
- `@FirebaseUser()` → Returns the **full decoded token** (`auth.DecodedIdToken`).
216+
- `@FirebaseUserClaims()` → Returns only the **custom claims** (roles/permissions) defined for the user.
217+
218+
This separation ensures that developers can access both the raw Firebase user object and the role/claims information independently.
219+
210220
## Resources
211221

212222
Check out a few resources that may come in handy when working with NestJS:

src/firebase/decorator/claims.decorator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ describe('Firebase Claims Decorator - Unit Test', () => {
3838

3939
const result = ClaimsFactory(null, mockExecutionContext);
4040

41-
expect(result).toEqual(mockClaims);
41+
expect(result).toEqual(mockClaims.claims);
4242
});
4343
});

src/firebase/decorator/claims.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { FIREBASE_CLAIMS_USER_METADATA } from '../constant/firebase.constant';
1212
export const ClaimsFactory = (data: unknown, ctx: ExecutionContext) => {
1313
const context = ctx.switchToHttp();
1414
const request = context.getRequest();
15-
return request.metadata?.[FIREBASE_CLAIMS_USER_METADATA as string];
15+
return request.metadata?.[FIREBASE_CLAIMS_USER_METADATA as string]?.claims;
1616
};
1717

1818
/**

src/firebase/decorator/user.decorator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ describe('Firebase User Decorator - Unit Test', () => {
3838

3939
const result = UserFactory(null, mockExecutionContext);
4040

41-
expect(result).toEqual(mockClaims);
41+
expect(result).toEqual(mockClaims.user);
4242
});
4343
});

src/firebase/decorator/user.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { FIREBASE_TOKEN_USER_METADATA } from '../constant/firebase.constant';
1212
export const UserFactory = (data: unknown, ctx: ExecutionContext) => {
1313
const context = ctx.switchToHttp();
1414
const request = context.getRequest();
15-
return request.metadata[FIREBASE_TOKEN_USER_METADATA as string];
15+
return request.metadata[FIREBASE_TOKEN_USER_METADATA as string]?.user;
1616
};
1717

1818
/**

test/app-local-validation.e2e-spec.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ describe('UsersController (e2e)', () => {
8888
.expect(200);
8989

9090
const responseBody = result.body;
91-
expect(responseBody).toHaveProperty('user');
92-
expect(responseBody.user).toHaveProperty('aud');
93-
expect(responseBody.user).toHaveProperty('user_id');
94-
expect(typeof responseBody.user.user_id).toBe('string');
95-
expect(responseBody.user).toHaveProperty('email');
96-
expect(typeof responseBody.user.email).toBe('string');
97-
expect(responseBody.user).toHaveProperty('firebase');
98-
expect(responseBody.user.firebase).toHaveProperty('sign_in_provider');
91+
expect(responseBody).toHaveProperty('aud');
92+
expect(responseBody).toHaveProperty('user_id');
93+
expect(typeof responseBody.user_id).toBe('string');
94+
expect(responseBody).toHaveProperty('email');
95+
expect(typeof responseBody.email).toBe('string');
96+
expect(responseBody).toHaveProperty('firebase');
97+
expect(responseBody.firebase).toHaveProperty('sign_in_provider');
9998
});
10099

101100
it('/users/set-claims (POST - Set claims)', async () => {
@@ -119,7 +118,7 @@ describe('UsersController (e2e)', () => {
119118
.expect(200);
120119

121120
const responseBody = response.body;
122-
expect(responseBody).toHaveProperty('claims', [Roles.ADMIN]);
121+
expect(responseBody).toHaveProperty([Roles.ADMIN]);
123122
});
124123

125124
it('/users/get-claims (GET - Get claims - 401)', async () => {

test/app.e2e-spec.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ describe('UsersController (e2e)', () => {
8787
.expect(200);
8888

8989
const responseBody = result.body;
90-
expect(responseBody).toHaveProperty('user');
91-
expect(responseBody.user).toHaveProperty('aud');
92-
expect(responseBody.user).toHaveProperty('user_id');
93-
expect(typeof responseBody.user.user_id).toBe('string');
94-
expect(responseBody.user).toHaveProperty('email');
95-
expect(typeof responseBody.user.email).toBe('string');
96-
expect(responseBody.user).toHaveProperty('firebase');
97-
expect(responseBody.user.firebase).toHaveProperty('sign_in_provider');
90+
expect(responseBody).toHaveProperty('aud');
91+
expect(responseBody).toHaveProperty('user_id');
92+
expect(typeof responseBody.user_id).toBe('string');
93+
expect(responseBody).toHaveProperty('email');
94+
expect(typeof responseBody.email).toBe('string');
95+
expect(responseBody).toHaveProperty('firebase');
96+
expect(responseBody.firebase).toHaveProperty('sign_in_provider');
9897
});
9998

10099
it('/users/set-claims (POST - Set claims)', async () => {
@@ -118,7 +117,7 @@ describe('UsersController (e2e)', () => {
118117
.expect(200);
119118

120119
const responseBody = response.body;
121-
expect(responseBody).toHaveProperty('claims', [Roles.ADMIN]);
120+
expect(responseBody).toHaveProperty([Roles.ADMIN]);
122121
});
123122

124123
it('/users/get-claims (GET - Get claims - 401)', async () => {

test/controller/user.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class UsersController {
3939
@RolesGuard(Roles.ADMIN)
4040
@Get('get-claims')
4141
async getClaims(@FirebaseUserClaims() claims: Roles[]) {
42-
return { ...claims };
42+
return claims;
4343
}
4444

4545
@UseGuards(FirebaseGuard)

0 commit comments

Comments
 (0)