feat: basic login

This commit is contained in:
Hasta Ragil Saputra 2021-12-08 16:28:13 +07:00
parent 141f3e0e5c
commit 8b9c43f0f8
2 changed files with 23 additions and 48 deletions

View File

@ -1,13 +1,14 @@
import { Controller, Post } from '@nestjs/common'; import { Controller, Post, UseGuards, Request } from '@nestjs/common';
import { InputLoginDto } from './dto/input-login.dto'; import { LocalAuthGuard } from './local-auth.guard';
import { AuthService } from './auth.service';
@Controller('auth') @Controller({
path: 'auth',
version: '1',
})
export class AuthController { export class AuthController {
constructor(private readonly authService: AuthService) {} @UseGuards(LocalAuthGuard)
@Post('login')
// @Post('login') async login(@Request() req) {
// public async login( @Body() loginUserDto: InputLoginDto): Promise<LoginStatus> { return req.user;
// return await this.authService.findByLogin(loginUserDto); }
// }
} }

View File

@ -1,46 +1,20 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service'; 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 { hashPassword } from '../helper/hash_password';
import { ResponseLoginDto } from './dto/response-login.dto';
@Injectable() @Injectable()
export class AuthService { export class AuthService {
constructor( constructor(private usersService: UsersService) {}
@InjectRepository(User)
private usersRepository: Repository<User>,
) {}
// async findByLogin({ username, password }: InputLoginDto): Promise<ResponseLoginDto> { async validateUser(username: string, pass: string): Promise<any> {
// const user = await this.usersRepository.findOne({ where: { username } }); const user = await this.usersService.findOneByUsername(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);
// }
if (user && user.password === (await hashPassword(pass, user.salt))) {
const { password, ...result } = user;
return result;
}
return null;
}
} }