Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ export class AppController {
}
}
```

#### Difference Between `@FirebaseUser` and `@FirebaseUserClaims`

> **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)):

- `@FirebaseUser()` → Returns the **full decoded token** (`auth.DecodedIdToken`).
- `@FirebaseUserClaims()` → Returns only the **custom claims** (roles/permissions) defined for the user.

This separation ensures that developers can access both the raw Firebase user object and the role/claims information independently.

## Resources

Check out a few resources that may come in handy when working with NestJS:
Expand Down
2 changes: 1 addition & 1 deletion src/firebase/decorator/claims.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ describe('Firebase Claims Decorator - Unit Test', () => {

const result = ClaimsFactory(null, mockExecutionContext);

expect(result).toEqual(mockClaims);
expect(result).toEqual(mockClaims.claims);
});
});
2 changes: 1 addition & 1 deletion src/firebase/decorator/claims.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FIREBASE_CLAIMS_USER_METADATA } from '../constant/firebase.constant';
export const ClaimsFactory = (data: unknown, ctx: ExecutionContext) => {
const context = ctx.switchToHttp();
const request = context.getRequest();
return request.metadata?.[FIREBASE_CLAIMS_USER_METADATA as string];
return request.metadata?.[FIREBASE_CLAIMS_USER_METADATA as string]?.claims;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/firebase/decorator/user.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ describe('Firebase User Decorator - Unit Test', () => {

const result = UserFactory(null, mockExecutionContext);

expect(result).toEqual(mockClaims);
expect(result).toEqual(mockClaims.user);
});
});
2 changes: 1 addition & 1 deletion src/firebase/decorator/user.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FIREBASE_TOKEN_USER_METADATA } from '../constant/firebase.constant';
export const UserFactory = (data: unknown, ctx: ExecutionContext) => {
const context = ctx.switchToHttp();
const request = context.getRequest();
return request.metadata[FIREBASE_TOKEN_USER_METADATA as string];
return request.metadata[FIREBASE_TOKEN_USER_METADATA as string]?.user;
};

/**
Expand Down
17 changes: 8 additions & 9 deletions test/app-local-validation.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ describe('UsersController (e2e)', () => {
.expect(200);

const responseBody = result.body;
expect(responseBody).toHaveProperty('user');
expect(responseBody.user).toHaveProperty('aud');
expect(responseBody.user).toHaveProperty('user_id');
expect(typeof responseBody.user.user_id).toBe('string');
expect(responseBody.user).toHaveProperty('email');
expect(typeof responseBody.user.email).toBe('string');
expect(responseBody.user).toHaveProperty('firebase');
expect(responseBody.user.firebase).toHaveProperty('sign_in_provider');
expect(responseBody).toHaveProperty('aud');
expect(responseBody).toHaveProperty('user_id');
expect(typeof responseBody.user_id).toBe('string');
expect(responseBody).toHaveProperty('email');
expect(typeof responseBody.email).toBe('string');
expect(responseBody).toHaveProperty('firebase');
expect(responseBody.firebase).toHaveProperty('sign_in_provider');
});

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

const responseBody = response.body;
expect(responseBody).toHaveProperty('claims', [Roles.ADMIN]);
expect(responseBody).toHaveProperty([Roles.ADMIN]);
});

it('/users/get-claims (GET - Get claims - 401)', async () => {
Expand Down
17 changes: 8 additions & 9 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,13 @@ describe('UsersController (e2e)', () => {
.expect(200);

const responseBody = result.body;
expect(responseBody).toHaveProperty('user');
expect(responseBody.user).toHaveProperty('aud');
expect(responseBody.user).toHaveProperty('user_id');
expect(typeof responseBody.user.user_id).toBe('string');
expect(responseBody.user).toHaveProperty('email');
expect(typeof responseBody.user.email).toBe('string');
expect(responseBody.user).toHaveProperty('firebase');
expect(responseBody.user.firebase).toHaveProperty('sign_in_provider');
expect(responseBody).toHaveProperty('aud');
expect(responseBody).toHaveProperty('user_id');
expect(typeof responseBody.user_id).toBe('string');
expect(responseBody).toHaveProperty('email');
expect(typeof responseBody.email).toBe('string');
expect(responseBody).toHaveProperty('firebase');
expect(responseBody.firebase).toHaveProperty('sign_in_provider');
});

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

const responseBody = response.body;
expect(responseBody).toHaveProperty('claims', [Roles.ADMIN]);
expect(responseBody).toHaveProperty([Roles.ADMIN]);
});

it('/users/get-claims (GET - Get claims - 401)', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class UsersController {
@RolesGuard(Roles.ADMIN)
@Get('get-claims')
async getClaims(@FirebaseUserClaims() claims: Roles[]) {
return { ...claims };
return claims;
}

@UseGuards(FirebaseGuard)
Expand Down