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

@@ -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;
}
}