feat(wip): login

This commit is contained in:
Hasta Ragil Saputra
2021-12-06 22:09:24 +07:00
parent 5a401ec862
commit 659c7e4de8
13 changed files with 306 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ import { TransactionModule } from './transaction/transaction.module';
import { ProductModule } from './product/product.module';
import { ConfigurableModule } from './configurable/configurable.module';
// import { AuthModule } from './auth/auth.module';
import { AuthModule } from './auth/auth.module';
import configuration from './config/configuration';
@Module({
@@ -48,11 +49,11 @@ import configuration from './config/configuration';
},
inject: [ConfigService],
}),
// UsersModule,
TransactionModule,
ConfigurableModule,
// AuthModule,
ProductModule,
UsersModule,
// TransactionModule,
// ConfigurableModule,
// ProductModule,
AuthModule,
],
})
export class AppModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';
describe('AuthController', () => {
let controller: AuthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
}).compile();
controller = module.get<AuthController>(AuthController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@@ -0,0 +1,11 @@
import { Controller, Request, Post, UseGuards } from '@nestjs/common';
import { LocalAuthGuard } from './local-auth.guard';
@Controller('auth')
export class AuthController {
@UseGuards(LocalAuthGuard)
@Post('login')
async login(@Request() req) {
return req.user;
}
}

13
src/auth/auth.module.ts Normal file
View File

@@ -0,0 +1,13 @@
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 { AuthController } from './auth.controller';
@Module({
imports: [UsersModule, PassportModule],
providers: [AuthService, LocalStrategy],
controllers: [AuthController],
})
export class AuthModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile();
service = module.get<AuthService>(AuthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

25
src/auth/auth.service.ts Normal file
View File

@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { InputLoginDto } from './dto/input-login.dto';
import { hashPassword } from '../helper/hash_password';
import { User } from '../users/entities/user.entity';
@Injectable()
export class AuthService {
constructor(private readonly usersService: UsersService) {}
async validateUser({
username,
password,
}: InputLoginDto): Promise<Omit<User, 'password'>> {
const user = await this.usersService.findOne(username);
if (user && user.password === (await hashPassword(password, user.salt))) {
const { password, ...result } = user;
return result;
}
return null;
}
}

View File

@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {}

View File

@@ -0,0 +1,28 @@
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';
import { User } from '../users/entities/user.entity';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
async validate(
username: string,
password: string,
): Promise<Omit<User, 'password'>> {
const user = await this.authService.validateUser({
username,
password,
});
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}

View File

@@ -3,7 +3,10 @@ import * as crypto from 'crypto';
export function hashPassword(password, salt): Promise<string> {
return new Promise((resolve, reject) => {
crypto.pbkdf2(password, salt, 50, 100, 'sha512', (err, values) => {
if (err) return reject(err);
if (err) {
return reject(err);
}
resolve(values.toString('hex'));
});
});

View File

@@ -8,5 +8,6 @@ import { User } from './entities/user.entity';
imports: [TypeOrmModule.forFeature([User])],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}

View File

@@ -4,6 +4,8 @@ import { UpdateUserDto } from './dto/update-user.dto';
import { EntityNotFoundError, Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { hashPassword } from '../helper/hash_password';
@Injectable()
export class UsersService {
@@ -13,7 +15,12 @@ export class UsersService {
) {}
async create(createUserDto: CreateUserDto) {
const result = await this.usersRepository.insert(createUserDto);
const salt = randomStringGenerator();
const result = await this.usersRepository.insert({
username: createUserDto.username,
password: await hashPassword(createUserDto.password, salt),
salt,
});
return this.usersRepository.findOneOrFail(result.identifiers[0].id);
}
@@ -81,4 +88,12 @@ export class UsersService {
await this.usersRepository.delete(id);
}
async findOneByUsername(username: string) {
return this.usersRepository.findOneOrFail({
where: {
username,
},
});
}
}