-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d16a14
commit 37ff88c
Showing
7 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { AuthService } from 'src/auth/auth.service'; | ||
import { AuthCredentialDto } from 'src/auth/dto/auth-credentials.dto'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@Post('/signup') | ||
signup(@Body() authCredentialDto: AuthCredentialDto): Promise<void> { | ||
return this.authService.signup(authCredentialDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { UsersRepository } from 'src/auth/users.repository'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([UsersRepository])], | ||
controllers: [AuthController], | ||
providers: [AuthService], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { AuthCredentialDto } from 'src/auth/dto/auth-credentials.dto'; | ||
import { UsersRepository } from 'src/auth/users.repository'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
@InjectRepository(UsersRepository) | ||
private usersRepository: UsersRepository, | ||
) {} | ||
|
||
async signup(authCredentialDto: AuthCredentialDto): Promise<void> { | ||
return await this.usersRepository.createUser(authCredentialDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IsString, Matches, MaxLength, MinLength } from 'class-validator'; | ||
|
||
export class AuthCredentialDto { | ||
@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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column({ unique: true }) | ||
username: string; | ||
|
||
@Column() | ||
password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
ConflictException, | ||
InternalServerErrorException, | ||
} from '@nestjs/common'; | ||
import { AuthCredentialDto } from 'src/auth/dto/auth-credentials.dto'; | ||
import { User } from 'src/auth/user.entity'; | ||
import { EntityRepository, Repository } from 'typeorm'; | ||
|
||
@EntityRepository(User) | ||
export class UsersRepository extends Repository<User> { | ||
async createUser(authCredentialDto: AuthCredentialDto): Promise<void> { | ||
const { username, password } = authCredentialDto; | ||
const user = this.create({ username, password }); | ||
try { | ||
await this.save(user); | ||
} catch (error) { | ||
if (Number(error.code) === 23505) { | ||
//duplicate username | ||
throw new ConflictException('username already exists'); | ||
} else { | ||
throw new InternalServerErrorException(); | ||
} | ||
} | ||
} | ||
} |