S
S
Speakermen2021-11-07 12:03:49
Express.js
Speakermen, 2021-11-07 12:03:49

How to validate Request() req?

For @Request() reqDto won't work as with @Body()?

import { Body, Controller, Get, Post, Request, UseGuards } from '@nestjs/common';
import { AuthService } from './auth/auth.service';
import { CreateRegisterDto } from './auth/dto/create-register.dto';
import { JwtAuthGuard } from './auth/guards/jwt-auth.guard';
import { LocalAuthGuard } from './auth/guards/local-auth.guard';

@Controller()
export class AppController {
  constructor(private readonly authService: AuthService) {}

  @UseGuards(LocalAuthGuard)
  @Post('/login')
  async login(@Request() req) {
    return req.user;
    //return this.authService.login();
    //return req.user;
    return this.authService.login(req.user);
  }

  //@UseGuards(LocalAuthGuard)
  @Post('/register')
  //async register(@Request() req) {
  async register(@Body() createRegisterDto: CreateRegisterDto) {
    return this.authService.register(createRegisterDto);
    //return this.authService.register(req.body);
  }

  @UseGuards(JwtAuthGuard)
  @Get('/profile')
  getProfile(@Request() req) {
    return req.user;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lssssssssssl, 2021-11-07
@Speakermen

Write a custom decorator for user, and make a dto for it.
https://docs.nestjs.com/custom-decorators

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question