Skip to content

Commit

Permalink
setup auth module
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaalanMarwan committed Apr 30, 2022
1 parent 7d16a14 commit 37ff88c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { TasksModule } from './tasks/tasks.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
@Module({
imports: [
TasksModule,
Expand All @@ -14,6 +15,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
autoLoadEntities: true,
synchronize: true,
}),
AuthModule,
],
})
export class AppModule {}
13 changes: 13 additions & 0 deletions src/auth/auth.controller.ts
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);
}
}
12 changes: 12 additions & 0 deletions src/auth/auth.module.ts
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 {}
16 changes: 16 additions & 0 deletions src/auth/auth.service.ts
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);
}
}
16 changes: 16 additions & 0 deletions src/auth/dto/auth-credentials.dto.ts
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;
}
13 changes: 13 additions & 0 deletions src/auth/user.entity.ts
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;
}
25 changes: 25 additions & 0 deletions src/auth/users.repository.ts
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();
}
}
}
}

0 comments on commit 37ff88c

Please sign in to comment.