import { Body, Controller, Post, UseGuards, Request } from '@nestjs/common';
import { UserService } from 'src/user/user.service';
import { LocalAuthGuard } from './auth.guard';
import { AuthService } from './auth.service';
import { RegisterDto } from './Dto/credentials.dto';

@Controller('auth')
export class AuthController {
  constructor(
    private readonly userService: UserService,
    private readonly authService: AuthService,
  ) {}
  @Post('register')
  async postRegister(@Body() registerDto: RegisterDto) {
    const user = await this.userService.create(registerDto);
    return user;
  }
  @UseGuards(LocalAuthGuard)
  @Post('login')
  async postLogin(@Request() request) {
    const { user } = request;
    const { access_token } = await this.authService.signToken(user);
    return {
      ...user,
      access_token,
    };
  }
}
