diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index ea04269..ca340e3 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,13 +1,14 @@ -import { Controller, Post } from '@nestjs/common'; -import { InputLoginDto } from './dto/input-login.dto'; -import { AuthService } from './auth.service'; +import { Controller, Post, UseGuards, Request } from '@nestjs/common'; +import { LocalAuthGuard } from './local-auth.guard'; -@Controller('auth') +@Controller({ + path: 'auth', + version: '1', +}) export class AuthController { - constructor(private readonly authService: AuthService) {} - - // @Post('login') - // public async login( @Body() loginUserDto: InputLoginDto): Promise { - // return await this.authService.findByLogin(loginUserDto); - // } + @UseGuards(LocalAuthGuard) + @Post('login') + async login(@Request() req) { + return req.user; + } } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 1216af8..0e1ffd9 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,46 +1,20 @@ -import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { UsersService } from '../users/users.service'; -import { InputLoginDto } from './dto/input-login.dto'; -import { InjectRepository } from '@nestjs/typeorm'; -import { User } from '../users/entities/user.entity'; -import { Repository } from 'typeorm'; import { hashPassword } from '../helper/hash_password'; -import { ResponseLoginDto } from './dto/response-login.dto'; @Injectable() export class AuthService { - constructor( - @InjectRepository(User) - private usersRepository: Repository, - ) {} + constructor(private usersService: UsersService) {} - // async findByLogin({ username, password }: InputLoginDto): Promise { - // const user = await this.usersRepository.findOne({ where: { username } }); - // - // if (!user) { - // throw new HttpException( - // { - // statusCode: HttpStatus.FORBIDDEN, - // error: 'Username not found', - // }, - // HttpStatus.FORBIDDEN, - // ); - // } - // - // // compare passwords - // const hashData = await hashPassword(password, user.salt); - // - // if( hashData != user.password ){ - // throw new HttpException( - // { - // statusCode: HttpStatus.FORBIDDEN, - // error: 'Password Not Match', - // }, - // HttpStatus.FORBIDDEN, - // ); - // } - // - // return ResponseLoginDto(user); - // } + async validateUser(username: string, pass: string): Promise { + const user = await this.usersService.findOneByUsername(username); + if (user && user.password === (await hashPassword(pass, user.salt))) { + const { password, ...result } = user; + + return result; + } + + return null; + } }