Skip to content

Commit

Permalink
Merge pull request #351 from bounswe/be-350/bugfix-lint-and-format
Browse files Browse the repository at this point in the history
Be 350/bugfix lint and format
  • Loading branch information
batuhancetin authored Nov 19, 2023
2 parents 2cf56b3 + 469856a commit ec68d22
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 246 deletions.
4 changes: 2 additions & 2 deletions app/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import { Moderator } from './moderator/entities/moderator.entity';
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
host: '172.17.0.2',
port: 5432,
password: 'password',
username: 'postgres',
entities: [User, Poll, Tag, Option,Moderator],
entities: [User, Poll, Tag, Option, Moderator],
database: 'postgres',
synchronize: true,
logging: true,
Expand Down
2 changes: 1 addition & 1 deletion app/backend/src/auth/dto/forgot-password.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';
import { IsEmail, IsNotEmpty } from 'class-validator';

export class ForgotPasswordDto {
@ApiProperty({
Expand Down
52 changes: 25 additions & 27 deletions app/backend/src/moderator/dto/create_moderator.dto.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsEmail, IsNotEmpty, IsString, MinLength } from "class-validator";
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';

export class CreateModeratorDto {
@ApiProperty({
uniqueItems: true,
example: '[email protected]',
})
@IsEmail()
@IsNotEmpty()
email: string;

@ApiProperty({
minLength: 6,
example: 'test11',
})
@IsString()
@MinLength(6)
password: string;

export class CreateModeratorDto {
@ApiProperty({
uniqueItems: true,
example: '[email protected]',
})
@IsEmail()
@IsNotEmpty()
email: string;

@ApiProperty({
minLength: 6,
example: 'test11',
})
@IsString()
@MinLength(6)
password: string;

@ApiProperty({
minLength: 6,
example: 'johnDoe',
})
@IsString()
@MinLength(6)
username: string;
}
@ApiProperty({
minLength: 6,
example: 'johnDoe',
})
@IsString()
@MinLength(6)
username: string;
}
33 changes: 16 additions & 17 deletions app/backend/src/moderator/dto/verify_moderator.dto.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsEmail, IsNotEmpty, IsNumber } from "class-validator";
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty, IsNumber } from 'class-validator';

export class VerifyModeratorDto {
@ApiProperty({
uniqueItems: true,
example: '[email protected]',
})
@IsEmail()
@IsNotEmpty()
email: string;

@ApiProperty({
example: 1234,
})
@IsNumber()
verificationCode: number;
}

@ApiProperty({
uniqueItems: true,
example: '[email protected]',
})
@IsEmail()
@IsNotEmpty()
email: string;

@ApiProperty({
example: 1234,
})
@IsNumber()
verificationCode: number;
}
1 change: 0 additions & 1 deletion app/backend/src/moderator/entities/moderator.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as bcrypt from 'bcrypt';
import { Poll } from '../../poll/entities/poll.entity';
import {
Entity,
Column,
Expand Down
99 changes: 54 additions & 45 deletions app/backend/src/moderator/guards/moderator.guard.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,70 @@
import {
CanActivate,
ExecutionContext,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Request } from 'express';
CanActivate,
ExecutionContext,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Request } from 'express';
import { ModeratorService } from '../moderator.service';

@Injectable()
export class ModeratorGuard implements CanActivate {
constructor(
private jwtService: JwtService,
private readonly moderatorService: ModeratorService,
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
const token = this.extractTokenFromHeader(request);
if (!token) {
throw new UnauthorizedException({

@Injectable()
export class ModeratorGuard implements CanActivate {
constructor(
private jwtService: JwtService,
private readonly moderatorService: ModeratorService,
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
const token = this.extractTokenFromHeader(request);
if (!token) {
throw new UnauthorizedException(
{
message: 'Token not found.',
data: null,
errors: null,
},
'401',);
}
try {
const payload = await this.jwtService.verifyAsync(
token,
'401',
);
}
try {
const payload = await this.jwtService.verifyAsync(token, {
secret: 'very-secret-key',
});

const moderator = await this.moderatorService.searchModerators(true, {
where: {
email: payload.email,
},
});

if (payload.userType != 1) {
throw new UnauthorizedException(
{
secret: 'very-secret-key'
}
);
const moderator = await this.moderatorService.searchModerator({email: payload.email});
if (payload.userType != 1) {
throw new UnauthorizedException({
message: 'User is not moderator.',
data: null,
errors: null,
},
'401',);
}
request['moderator'] = moderator;
} catch {
throw new UnauthorizedException({
'401',
);
}
request['moderator'] = moderator;
} catch {
throw new UnauthorizedException(
{
message: 'Token is invalid.',
data: null,
errors: null,
},
'401',);
}
return true;
}

private extractTokenFromHeader(request: Request): string | undefined {
const [type, token] = request.headers.authorization?.split(' ') ?? [];
return type === 'Bearer' ? token : undefined;
'401',
);
}
}
return true;
}

private extractTokenFromHeader(request: Request): string | undefined {
const [type, token] = request.headers.authorization?.split(' ') ?? [];
return type === 'Bearer' ? token : undefined;
}
}
50 changes: 24 additions & 26 deletions app/backend/src/moderator/guards/verification.guard.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import {
Injectable,
CanActivate,
ExecutionContext,
UnauthorizedException,
} from '@nestjs/common';

@Injectable()
export class VerificationGuard implements CanActivate {
constructor() {}

public async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();


if (!request.moderator.isVerified) {
throw new UnauthorizedException(
{
message: 'Moderator is not verified',
data: null,
errors: null,
},
'401',
);
}
return true;
Injectable,
CanActivate,
ExecutionContext,
UnauthorizedException,
} from '@nestjs/common';

@Injectable()
export class VerificationGuard implements CanActivate {
constructor() {}

public async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();

if (!request.moderator.isVerified) {
throw new UnauthorizedException(
{
message: 'Moderator is not verified',
data: null,
errors: null,
},
'401',
);
}
return true;
}
}
43 changes: 35 additions & 8 deletions app/backend/src/moderator/moderator.controller.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
import { Body, Controller, Get, Post } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
UseGuards,
} from '@nestjs/common';
import { ModeratorService } from './moderator.service';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CreateModeratorDto } from './dto/create_moderator.dto';
import { Verify } from 'crypto';
import { VerifyModeratorDto } from './dto/verify_moderator.dto';
import { ModeratorGuard } from './guards/moderator.guard';
import { VerificationGuard } from './guards/verification.guard';

@ApiBearerAuth()
@Controller('moderator')
@ApiTags('moderator')
export class ModeratorController {
constructor(private readonly moderatorService: ModeratorService) {}

@Post('register')
createModerator(@Body() createModeratorDto: CreateModeratorDto) {
return this.moderatorService.createModerator(createModeratorDto);
}

@Post('register/verify')
verifyModerator(@Body() verifyModeratorDto: VerifyModeratorDto) {
return this.moderatorService.verifyModerator(verifyModeratorDto);
}

@Get()
findAll() {
return this.moderatorService.findAll();
}

@UseGuards(ModeratorGuard, VerificationGuard)
@Get('polls')
fetchUnapprovedPolls() {
return this.moderatorService.fetchUnapprovedPolls();
@Post("register")
createModerator (@Body() createModeratorDto: CreateModeratorDto){
return this.moderatorService.createModerator(createModeratorDto)
}

@Post("register/verify")
verifyModerator (@Body() verifyModeratorDto: VerifyModeratorDto){
return this.moderatorService.verifyModerator(verifyModeratorDto)
@Get(':id')
findOne(@Param('id') id: string) {
return this.moderatorService.findOneById(id);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.moderatorService.removeById(id);
}
}
13 changes: 10 additions & 3 deletions app/backend/src/moderator/moderator.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import { ModeratorService } from './moderator.service';
import { ModeratorController } from './moderator.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Moderator } from './entities/moderator.entity';
import { Poll } from '../poll/entities/poll.entity';
import { JwtModule } from '@nestjs/jwt';

@Module({
imports: [TypeOrmModule.forFeature([Moderator])],
imports: [
TypeOrmModule.forFeature([Moderator, Poll]),
JwtModule.register({
global: true,
secret: 'very-secret-key',
signOptions: { expiresIn: '10m' },
}),
],
controllers: [ModeratorController],
providers: [ModeratorService],
})
export class ModeratorModule {}


Loading

0 comments on commit ec68d22

Please sign in to comment.