From f1594ebd9611ff0d09e838bd8b4fc33800e1fe3c Mon Sep 17 00:00:00 2001 From: ilham Date: Mon, 6 Dec 2021 22:31:11 +0700 Subject: [PATCH] add:authentication --- src/auth/auth.controller.ts | 16 ++++++----- src/auth/auth.module.ts | 22 ++++++++++++--- src/auth/auth.service.ts | 53 ++++++++++++++++++++++++++----------- 3 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 605fe1d..ea04269 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,11 +1,13 @@ -import { Controller, Request, Post, UseGuards } from '@nestjs/common'; -import { LocalAuthGuard } from './local-auth.guard'; +import { Controller, Post } from '@nestjs/common'; +import { InputLoginDto } from './dto/input-login.dto'; +import { AuthService } from './auth.service'; @Controller('auth') export class AuthController { - @UseGuards(LocalAuthGuard) - @Post('login') - async login(@Request() req) { - return req.user; - } + constructor(private readonly authService: AuthService) {} + + // @Post('login') + // public async login( @Body() loginUserDto: InputLoginDto): Promise { + // return await this.authService.findByLogin(loginUserDto); + // } } diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 8d2d164..7213c5d 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -2,12 +2,28 @@ import { Module } from '@nestjs/common'; import { AuthService } from './auth.service'; import { UsersModule } from '../users/users.module'; import { PassportModule } from '@nestjs/passport'; -import { LocalStrategy } from './local.strategy'; +import { JwtModule, JwtStrategy } from 'passport-jwt'; import { AuthController } from './auth.controller'; @Module({ - imports: [UsersModule, PassportModule], - providers: [AuthService, LocalStrategy], + imports: [ + UsersModule, + PassportModule.register({ + defaultStrategy: 'jwt', + property: 'user', + session: false, + }), + JwtModule.register({ + secret: process.env.SECRETKEY, signOptions: { + expiresIn: process.env.EXPIRESIN, + }, + }), + ], controllers: [AuthController], + providers: [AuthService, JwtStrategy], + exports: [ + PassportModule, + JwtModule + ], }) export class AuthModule {} diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index b8f30dc..1216af8 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,25 +1,46 @@ -import { Injectable } from '@nestjs/common'; +import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { UsersService } from '../users/users.service'; import { InputLoginDto } from './dto/input-login.dto'; -import { hashPassword } from '../helper/hash_password'; +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(private readonly usersService: UsersService) {} + constructor( + @InjectRepository(User) + private usersRepository: Repository, + ) {} - async validateUser({ - username, - password, - }: InputLoginDto): Promise> { - const user = await this.usersService.findOne(username); + // 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); + // } - if (user && user.password === (await hashPassword(password, user.salt))) { - const { password, ...result } = user; - - return result; - } - - return null; - } }