Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/typeorm": "^8.0.2",
"bcrypt": "^5.0.1",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"pg": "^8.7.1",
Expand Down
5 changes: 5 additions & 0 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ export class AuthController {
signUp(@Body() authCredentialsDto: AuthCredentialsDto): Promise<void> {
return this.authService.signUp(authCredentialsDto);
}

@Post('/signin')
signIn(@Body() authCredentialsDto: AuthCredentialsDto): Promise<string> {
return this.authService.signIn(authCredentialsDto);
}
}
14 changes: 13 additions & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import { UsersRepository } from './users.repository';
import * as bcrypt from 'bcrypt';

@Injectable()
export class AuthService {
Expand All @@ -13,4 +14,15 @@ export class AuthService {
async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
return this.usersRepository.createUser(authCredentialsDto);
}

async signIn(authCredentialsDto: AuthCredentialsDto): Promise<string> {
const { username, password } = authCredentialsDto;
const user = await this.usersRepository.findOne({ username });

if (user && (await bcrypt.compare(password, user.password))) {
return 'Success!';
} else {
throw new UnauthorizedException('Please check your login credentials.');
}
}
}
12 changes: 12 additions & 0 deletions src/auth/dto/auth-credentials.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { IsString, Matches, MaxLength, MinLength } from 'class-validator';

export class AuthCredentialsDto {
@IsString()
@MinLength(4)
@MaxLength(20)
username: string;

@IsString()
@MinLength(8)
@MaxLength(32)
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
message: 'Password is too weak.',
})
password: string;
}
4 changes: 2 additions & 2 deletions src/auth/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User {
@PrimaryGeneratedColumn()
@PrimaryGeneratedColumn('uuid')
id: string;

@Column()
@Column({ unique: true })
username: string;

@Column()
Expand Down
21 changes: 19 additions & 2 deletions src/auth/users.repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import {
ConflictException,
InternalServerErrorException,
} from '@nestjs/common';
import { EntityRepository, Repository } from 'typeorm';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import { User } from './user.entity';
import * as bcrypt from 'bcrypt';

@EntityRepository(User)
export class UsersRepository extends Repository<User> {
async createUser(authCredentialsDto: AuthCredentialsDto): Promise<void> {
const { username, password } = authCredentialsDto;
const user = this.create({ username, password });
await this.save(user);

const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(password, salt);
const user = this.create({ username, password: hashedPassword });

try {
await this.save(user);
} catch (error) {
if (error.code === '23505') {
throw new ConflictException('Username already exists.');
} else {
throw new InternalServerErrorException();
}
}
}
}
Loading