Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication: Hashing password #38

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"@team8/constants": "*",
"@team8/types": "*",
"@team8/utils": "*",
"bcrypt": "^5.1.1",
"i": "^0.3.7",
"mysql2": "^3.9.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
Expand All @@ -38,9 +40,11 @@
"@nestjs/testing": "^10.0.0",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.64",
"@types/bcrypt": "^5.0.2",
"@types/express": "^4.17.17",
"@types/supertest": "^2.0.12",
"eslint-config-team8": "*",
"i": "^0.3.7",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3"
}
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Post, HttpCode, HttpStatus } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LogInDto } from '@team8/types/dtos/auth/login.dto';
import { SignUpDto } from '@team8/types/dtos/auth/signup.dto';
Expand All @@ -12,6 +12,7 @@ export class AuthController {
return this.authService.signUp(signupDto);
}

@HttpCode(HttpStatus.OK)
@Post('login')
logIn(@Body() logInDto: LogInDto) {
return this.authService.logIn(logInDto);
Expand Down
10 changes: 7 additions & 3 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import {
LogInRetDto,
SignUpRetDto,
} from '@team8/types/dtos/auth';
import * as bcrypt from 'bcrypt';
import { CreateUserDto } from './createUser.dto';

@Injectable()
export class AuthService {
constructor(private usersService: UsersService) {}

async signUp(dto: SignUpDto): Promise<SignUpRetDto> {
//TODO: Hash the password
const user = await this.usersService.create(dto);
const saltOrRounds = 10;
const userDto = new CreateUserDto(dto);
userDto.hashPassword = await bcrypt.hash(dto.password, saltOrRounds);
const user = await this.usersService.create(userDto);
const result = new SignUpRetDto();
result.username = user.username;
return result;
Expand All @@ -22,7 +26,7 @@ export class AuthService {
async logIn(dto: LogInDto): Promise<LogInRetDto> {
//TODO: Return message according to error
const user = await this.usersService.findOneByUsername(dto.username);
if (user?.hashPassword !== dto.hashPassword) {
if (!(await bcrypt.compare(dto.password, user.hashPassword))) {
throw new UnauthorizedException();
}
const result = new LogInRetDto();
Expand Down
19 changes: 19 additions & 0 deletions apps/backend/src/auth/createUser.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsNotEmpty, IsString } from 'class-validator';

export class CreateUserDto {
@IsString()
@IsNotEmpty()
username: string;

@IsString()
@IsNotEmpty()
fullName: string;

@IsString()
@IsNotEmpty()
hashPassword: string;

constructor(dto: Partial<CreateUserDto>) {
Object.assign(this, dto);
}
}
3 changes: 2 additions & 1 deletion apps/backend/src/profile/profile.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Post, HttpCode, HttpStatus } from '@nestjs/common';
import { ProfileService } from './profile.service';
import { UpdateDto } from '@team8/types/dtos/profile/update.dto';

Expand All @@ -7,6 +7,7 @@ export class ProfileController {
constructor(private profileService: ProfileService) {}

//TODO: Create update function for each field of user profile
@HttpCode(HttpStatus.OK)
@Post()
update(@Body() updateDto: UpdateDto) {
return this.profileService.update(updateDto);
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Repository, QueryFailedError } from 'typeorm';
import { User } from '../entities/user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { UpdateDto } from '@team8/types/dtos/profile/update.dto';
import { CreateUserDto } from '../auth/createUser.dto';

@Injectable()
export class UsersService {
Expand All @@ -12,7 +13,7 @@ export class UsersService {
private readonly usersRepository: Repository<User>,
) {}

async create(dto: SignUpDto) {
async create(dto: CreateUserDto) {
try {
await this.usersRepository.save(dto);
return await this.findOneByUsername(dto.username);
Expand Down
Loading