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 { 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<LoginStatus> {
// return await this.authService.findByLogin(loginUserDto);
// }
@UseGuards(LocalAuthGuard)
@Post('login')
async login(@Request() req) {
return req.user;
}
}

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 { 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<User>,
) {}
constructor(private usersService: UsersService) {}
// async findByLogin({ username, password }: InputLoginDto): Promise<ResponseLoginDto> {
// 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<any> {
const user = await this.usersService.findOneByUsername(username);
if (user && user.password === (await hashPassword(pass, user.salt))) {
const { password, ...result } = user;
return result;
}
return null;
}
}