// user.decorator.ts
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { JWTPayload } from '@/api/auth/auth.service';
export const User = createParamDecorator(
(userField: keyof JWTPayload, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
const user: JWTPayload | undefined = request.user;
return userField ? user?.[userField] : user;
},
);
export class AuthController {
constructor(private authService: AuthService) {}
@UseGuards(AuthGuard("jwt"))
@Get()
getPayload(@User() user: JWTPayload) {
// здесь мне нужно получить payload который был возвращен из jwt стратегии.
}
@UseGuards(AuthGuard("jwt"))
@Get(':id')
getUserId(@User('id') userId: string) {
// здесь мне нужно получить payload который был возвращен из jwt стратегии.
}
}
nestjs-i18n
.@Injectable()
export class AuthService {
constructor(
@InjectRepository(UserEntity)
private readonly userRepository: Repository<UserEntity>,
private readonly jwtService: JwtService,
private readonly configService: ConfigService<EnvironmentVariables>,
private readonly userService: UserService,
private readonly i18n: I18nRequestScopeService, // Проблема тут
) {}
@Injectable()
export class AuthService {
constructor(
@InjectRepository(UserEntity)
private readonly userRepository: Repository<UserEntity>,
private readonly jwtService: JwtService,
private readonly configService: ConfigService<EnvironmentVariables>,
private readonly userService: UserService,
private readonly i18n: I18nService, // Меняем на I18nService и всё успешно работает ))
) {}
const data = {
id: 0,
conversationMessageId: 0,
peerId: 0,
peerType: 'chat',
senderId: 0,
senderType: 'user',
createdAt: 0,
updatedAt: undefined,
pinnedAt: undefined,
text: undefined,
forwards: [
{
id: 0,
conversationMessageId: 0,
peerId: 0,
peerType: 'chat',
senderId: 0,
senderType: 'user',
createdAt: 0,
updatedAt: undefined,
pinnedAt: undefined,
text: undefined,
forwards: [
{
id: 0,
conversationMessageId: 0,
peerId: 0,
peerType: 'chat',
senderId: 0,
senderType: 'user',
createdAt: 0,
updatedAt: undefined,
pinnedAt: undefined,
text: undefined,
forwards: [],
attachments: [
{
id: 0,
ownerId: 0,
accessKey: '000000',
duration: 0,
oggUrl: 'https://psv4.userapi.com/c0//u0/audiomsg/d4/0.ogg',
mp3Url: 'https://psv4.userapi.com/c0//u0/audiomsg/d4/0.mp3',
url: 'https://psv4.userapi.com/c0//u0/audiomsg/d4/0.mp3',
locale: undefined,
transcript: undefined,
isTranscriptDone: false
}
],
isOutbox: false,
type: 'message',
subTypes: [
'message_new'
],
state: {}
}
],
attachments: [],
isOutbox: false,
type: 'message',
subTypes: [
'message_new'
],
state: {}
}
],
attachments: [],
isOutbox: true,
type: 'message',
subTypes: [
'message_new'
],
state: {}
};
console.log(data.forwards[0].forwards[0].attachments)