Skip to content

Commit

Permalink
'.'
Browse files Browse the repository at this point in the history
  • Loading branch information
ManucherKM committed Oct 12, 2023
1 parent 2cd77a7 commit df929f7
Show file tree
Hide file tree
Showing 21 changed files with 343 additions and 17 deletions.
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"mongoose": "^7.5.1",
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.5",
"otp-generator": "^4.0.1",
"passport-jwt": "^4.0.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
Expand All @@ -52,6 +53,7 @@
"@types/jsonwebtoken": "^9.0.2",
"@types/multer": "^1.4.7",
"@types/nodemailer": "^6.4.10",
"@types/otp-generator": "^4.0.0",
"@types/passport-jwt": "^3.0.9",
"@types/supertest": "^2.0.11",
"@types/uuid": "^9.0.3",
Expand Down
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ActivationModule } from './activation/activation.module'
import { ArchiveModule } from './archive/archive.module'
import { AuthModule } from './auth/auth.module'
import { FileModule } from './file/file.module'
import { OtpModule } from './otp/otp.module'
import { RestoreAccountModule } from './restore-account/restore-account.module'

// App
@Module({
Expand All @@ -25,6 +27,8 @@ import { FileModule } from './file/file.module'
ActivationModule,
FileModule,
ArchiveModule,
OtpModule,
RestoreAccountModule,
],
})
export class AppModule {}
20 changes: 10 additions & 10 deletions src/guard/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { JwtService } from '@/jwt/jwt.service'
import {
CanActivate,
ExecutionContext,
Inject,
Injectable,
} from '@nestjs/common'
import { getDataByToken } from '@/utils/getDataByToken'
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'

@Injectable()
export class JwtAuthGuard implements CanActivate {
constructor(@Inject(JwtService) private readonly jwtService: JwtService) {}
constructor() {}

async canActivate(context: ExecutionContext): Promise<boolean> {
try {
Expand All @@ -19,11 +14,16 @@ export class JwtAuthGuard implements CanActivate {
return false
}

const [isValid] = await this.jwtService.validateToken(token, 'access')
const data = getDataByToken(token, 'access')

return isValid
if (!data) {
return false
}

return true
} catch (e) {
console.error(e)
return false
}
}
}
6 changes: 6 additions & 0 deletions src/otp/dto/create-otp.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsEmail, IsNotEmpty, IsString } from 'class-validator'

export class CreateOtpDto {
@IsEmail()
email: string
}
4 changes: 4 additions & 0 deletions src/otp/dto/update-otp.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger'
import { CreateOtpDto } from './create-otp.dto'

export class UpdateOtpDto extends PartialType(CreateOtpDto) {}
16 changes: 16 additions & 0 deletions src/otp/entities/otp.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { HydratedDocument } from 'mongoose'

export type OtpDocument = HydratedDocument<Otp>

@Schema({
timestamps: true,
})
export class Otp {
@Prop({ required: true, unique: true })
email: string
@Prop({ required: true, unique: true })
otp: number
}

export const OtpSchema = SchemaFactory.createForClass(Otp)
28 changes: 28 additions & 0 deletions src/otp/otp.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Body,
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
Patch,
Post,
} from '@nestjs/common'
import { CreateOtpDto } from './dto/create-otp.dto'
import { UpdateOtpDto } from './dto/update-otp.dto'
import { OtpService } from './otp.service'

@Controller('otp')
export class OtpController {
constructor(private readonly otpService: OtpService) {}

@Post()
async create(@Body() createOtpDto: CreateOtpDto) {
try {
return await this.otpService.create(createOtpDto)
} catch (e) {
throw new HttpException({ message: e.message }, HttpStatus.BAD_REQUEST)
}
}
}
13 changes: 13 additions & 0 deletions src/otp/otp.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { Otp, OtpSchema } from './entities/otp.entity'
import { OtpController } from './otp.controller'
import { OtpService } from './otp.service'

@Module({
imports: [MongooseModule.forFeature([{ name: Otp.name, schema: OtpSchema }])],
controllers: [OtpController],
providers: [OtpService],
exports: [OtpService],
})
export class OtpModule {}
52 changes: 52 additions & 0 deletions src/otp/otp.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getOtp } from '@/utils/getOtp'
import { BadRequestException, Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model } from 'mongoose'
import { CreateOtpDto } from './dto/create-otp.dto'
import { UpdateOtpDto } from './dto/update-otp.dto'
import { Otp } from './entities/otp.entity'

@Injectable()
export class OtpService {
constructor(
@InjectModel(Otp.name)
private readonly otpModel: Model<Otp>,
) {}

async create({ email }: CreateOtpDto) {
const foundOtp = await this.findbyEmail(email)

if (foundOtp) {
return foundOtp.otp
}

const otp = await this.generateOtp(email)

await this.otpModel.create({
email,
otp,
})

return otp
}

async findbyEmail(email: string) {
return await this.otpModel.findOne({ email })
}

async findbyOtp(otp: number) {
return await this.otpModel.findOne({ otp })
}

async generateOtp(email: string): Promise<number> {
const otp = getOtp(6)

const foundOtp = await this.findbyOtp(otp)

if (foundOtp) {
return await this.generateOtp(email)
}

return otp
}
}
7 changes: 7 additions & 0 deletions src/restore-account/dto/create-restore-account.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsNotEmpty, IsString } from 'class-validator'

export class CreateRestoreAccountDto {
@IsNotEmpty()
@IsString()
email: string
}
6 changes: 6 additions & 0 deletions src/restore-account/dto/update-restore-account.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { PartialType } from '@nestjs/swagger'
import { CreateRestoreAccountDto } from './create-restore-account.dto'

export class UpdateRestoreAccountDto extends PartialType(
CreateRestoreAccountDto,
) {}
9 changes: 9 additions & 0 deletions src/restore-account/dto/verification-otp.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IsEmail, IsNotEmpty, IsNumber, IsString } from 'class-validator'

export class VerificationOtpDto {
@IsEmail()
email: string

@IsNumber()
otp: number
}
1 change: 1 addition & 0 deletions src/restore-account/entities/restore-account.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class RestoreAccount {}
53 changes: 53 additions & 0 deletions src/restore-account/restore-account.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
} from '@nestjs/common'
import { ApiBody, ApiTags } from '@nestjs/swagger'
import { CreateRestoreAccountDto } from './dto/create-restore-account.dto'
import { UpdateRestoreAccountDto } from './dto/update-restore-account.dto'
import { VerificationOtpDto } from './dto/verification-otp.dto'
import { RestoreAccountService } from './restore-account.service'

@ApiTags('Restore account')
@Controller('restore-account')
export class RestoreAccountController {
constructor(private readonly restoreAccountService: RestoreAccountService) {}

@Post()
@ApiBody({
schema: {
type: 'object',
properties: {
email: {
default: '[email protected]',
},
},
},
})
async createOtp(@Body() createRestoreAccountDto: CreateRestoreAccountDto) {
return await this.restoreAccountService.createOtp(createRestoreAccountDto)
}

@ApiBody({
schema: {
type: 'object',
properties: {
email: {
default: '[email protected]',
},
otp: {
default: '636421',
},
},
},
})
@Post('verification')
async verificationOtp(@Body() verificationOtpDto: VerificationOtpDto) {
return await this.restoreAccountService.verificationOtp(verificationOtpDto)
}
}
13 changes: 13 additions & 0 deletions src/restore-account/restore-account.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { JwtModule } from '@/jwt/jwt.module'
import { OtpModule } from '@/otp/otp.module'
import { UserModule } from '@/user/user.module'
import { Module } from '@nestjs/common'
import { RestoreAccountController } from './restore-account.controller'
import { RestoreAccountService } from './restore-account.service'

@Module({
imports: [OtpModule, UserModule, JwtModule],
controllers: [RestoreAccountController],
providers: [RestoreAccountService],
})
export class RestoreAccountModule {}
Loading

0 comments on commit df929f7

Please sign in to comment.