-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from bounswe/be-350/bugfix-lint-and-format
Be 350/bugfix lint and format
- Loading branch information
Showing
14 changed files
with
306 additions
and
246 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
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 |
---|---|---|
@@ -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; | ||
} |
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 |
---|---|---|
@@ -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; | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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
Oops, something went wrong.