You can avoid directly extending the Request interface and enhance type safety when accessing user data, you can create a custom parameter decorator.
Create user.decorator.ts file in src/common/decorator or at any place and add this code:
import {User} from '../../common/decorators/user.decorator';
@User() user: { userId: string; email: string; roleId: string } //instead of @Req user: Request
more clear example:
@UseGuards(AuthGuard('jwt'))
@Get('email')
getUserEmail(@User() user: { userId: string; email: string; roleId: string }) {
// your remaining logic
}
you can use all the attributes exit in user. Using that fix you don't further need to use Request Type from express and it work in deployment too and provides strong type safety.
Top comments (0)