add:authentication
This commit is contained in:
parent
6622501b61
commit
f1594ebd96
|
@ -1,11 +1,13 @@
|
||||||
import { Controller, Request, Post, UseGuards } from '@nestjs/common';
|
import { Controller, Post } from '@nestjs/common';
|
||||||
import { LocalAuthGuard } from './local-auth.guard';
|
import { InputLoginDto } from './dto/input-login.dto';
|
||||||
|
import { AuthService } from './auth.service';
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
@UseGuards(LocalAuthGuard)
|
constructor(private readonly authService: AuthService) {}
|
||||||
@Post('login')
|
|
||||||
async login(@Request() req) {
|
// @Post('login')
|
||||||
return req.user;
|
// public async login( @Body() loginUserDto: InputLoginDto): Promise<LoginStatus> {
|
||||||
}
|
// return await this.authService.findByLogin(loginUserDto);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,28 @@ import { Module } from '@nestjs/common';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { UsersModule } from '../users/users.module';
|
import { UsersModule } from '../users/users.module';
|
||||||
import { PassportModule } from '@nestjs/passport';
|
import { PassportModule } from '@nestjs/passport';
|
||||||
import { LocalStrategy } from './local.strategy';
|
import { JwtModule, JwtStrategy } from 'passport-jwt';
|
||||||
import { AuthController } from './auth.controller';
|
import { AuthController } from './auth.controller';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [UsersModule, PassportModule],
|
imports: [
|
||||||
providers: [AuthService, LocalStrategy],
|
UsersModule,
|
||||||
|
PassportModule.register({
|
||||||
|
defaultStrategy: 'jwt',
|
||||||
|
property: 'user',
|
||||||
|
session: false,
|
||||||
|
}),
|
||||||
|
JwtModule.register({
|
||||||
|
secret: process.env.SECRETKEY, signOptions: {
|
||||||
|
expiresIn: process.env.EXPIRESIN,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
controllers: [AuthController],
|
controllers: [AuthController],
|
||||||
|
providers: [AuthService, JwtStrategy],
|
||||||
|
exports: [
|
||||||
|
PassportModule,
|
||||||
|
JwtModule
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class AuthModule {}
|
export class AuthModule {}
|
||||||
|
|
|
@ -1,25 +1,46 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { UsersService } from '../users/users.service';
|
import { UsersService } from '../users/users.service';
|
||||||
import { InputLoginDto } from './dto/input-login.dto';
|
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 { User } from '../users/entities/user.entity';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
|
import { hashPassword } from '../helper/hash_password';
|
||||||
|
import { ResponseLoginDto } from './dto/response-login.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
constructor(private readonly usersService: UsersService) {}
|
constructor(
|
||||||
|
@InjectRepository(User)
|
||||||
|
private usersRepository: Repository<User>,
|
||||||
|
) {}
|
||||||
|
|
||||||
async validateUser({
|
// async findByLogin({ username, password }: InputLoginDto): Promise<ResponseLoginDto> {
|
||||||
username,
|
// const user = await this.usersRepository.findOne({ where: { username } });
|
||||||
password,
|
//
|
||||||
}: InputLoginDto): Promise<Omit<User, 'password'>> {
|
// if (!user) {
|
||||||
const user = await this.usersService.findOne(username);
|
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user