-
Notifications
You must be signed in to change notification settings - Fork 3
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 #22 from p-society/auth-otp
Addition of Otp flow to project.
- Loading branch information
Showing
33 changed files
with
407 additions
and
254 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as bcrypt from 'bcryptjs'; | ||
|
||
export async function hashString(input: string): Promise<string> { | ||
const saltRounds = 10; | ||
const hashedString = await bcrypt.hash(input, saltRounds); | ||
return hashedString; | ||
} | ||
|
||
export async function compareHashedString( | ||
input: string, | ||
hashedString: string, | ||
): Promise<boolean> { | ||
const isMatch = await bcrypt.compare(input, hashedString); | ||
return isMatch; | ||
} |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export const OTP_TTL = 10 * 60; // 10 minutes |
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,22 +1,22 @@ | ||
import { Controller, Post } from '@nestjs/common'; | ||
import { MailerService } from './mailer.service'; | ||
// import { Controller, Post } from '@nestjs/common'; | ||
// import { MailerService } from './mailer.service'; | ||
|
||
@Controller('mail') | ||
export class MailerController { | ||
constructor(private mailerService: MailerService) {} | ||
// @Controller('mail') | ||
// export class MailerController { | ||
// constructor(private mailerService: MailerService) {} | ||
|
||
// @Public() | ||
// @Post() | ||
// async sendMail() { | ||
// await this.mailerService.sendMPassGenerationEmail( | ||
// '[email protected]', | ||
// 'Saswat', | ||
// 'XYZ' | ||
// ) | ||
// await this.mailerService.sendSupportTicketResolvedEmail( | ||
// '[email protected]', | ||
// 'Saswat', | ||
// '123456' | ||
// ) | ||
// } | ||
} | ||
// // @Public() | ||
// // @Post() | ||
// // async sendMail() { | ||
// // await this.mailerService.sendMPassGenerationEmail( | ||
// // '[email protected]', | ||
// // 'Saswat', | ||
// // 'XYZ' | ||
// // ) | ||
// // await this.mailerService.sendSupportTicketResolvedEmail( | ||
// // '[email protected]', | ||
// // 'Saswat', | ||
// // '123456' | ||
// // ) | ||
// // } | ||
// } |
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 |
---|---|---|
|
@@ -9,17 +9,20 @@ export class MailerService { | |
|
||
async sendMail( | ||
to: string, | ||
subject: string, | ||
content: string, | ||
// subject: string, | ||
// content: string, | ||
otp: string, | ||
template?: string, | ||
context?: Record<string, any>, | ||
//context?: Record<string, any>, | ||
) { | ||
await this.mailerService.sendMail({ | ||
to, | ||
subject, | ||
text: content, | ||
subject: 'OTP for your account', | ||
template: template ? `./${template}` : undefined, | ||
context: context || {}, | ||
context: { | ||
otp, | ||
supportEmail: '[email protected]', | ||
}, | ||
}); | ||
} | ||
} |
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,6 +1,68 @@ | ||
<p> Use | ||
the OTP below to complete the process:</p> | ||
<div class='otp'> {{otp}} </div> | ||
<p>This OTP is valid for 15 minutes. If you didn’t request this, please ignore | ||
this email or contact support at | ||
{{supportEmail}}</p> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #f4f4f4; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
.container { | ||
max-width: 600px; | ||
margin: 20px auto; | ||
background-color: #ffffff; | ||
padding: 20px; | ||
border-radius: 8px; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
} | ||
.header { | ||
text-align: center; | ||
padding-bottom: 20px; | ||
border-bottom: 1px solid #dddddd; | ||
} | ||
.header h1 { | ||
margin: 0; | ||
color: #333333; | ||
} | ||
.content { | ||
padding: 20px 0; | ||
} | ||
.otp { | ||
font-size: 24px; | ||
font-weight: bold; | ||
color: #333333; | ||
text-align: center; | ||
margin: 20px 0; | ||
} | ||
.footer { | ||
text-align: center; | ||
padding-top: 20px; | ||
border-top: 1px solid #dddddd; | ||
color: #777777; | ||
} | ||
.footer a { | ||
color: #007BFF; | ||
text-decoration: none; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="header"> | ||
<h1>Programming Society</h1> | ||
</div> | ||
<div class="content"> | ||
<p>Dear Member,</p> | ||
<p>Use the OTP below to complete the process:</p> | ||
<div class="otp">{{otp}}</div> | ||
<p>This OTP is valid for 10 minutes. If you didn’t request this, please ignore this email or contact support at <a href="mailto:{{supportEmail}}">{{supportEmail}}</a>.</p> | ||
</div> | ||
<div class="footer"> | ||
<p>Thank you,<br>Programming Society Team</p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
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,23 @@ | ||
import { date, z } from 'zod'; | ||
|
||
// DTO for incoming OTP payload. This ensures the payload structure and validates fields. | ||
/** | ||
* The email of the user to send OTP. | ||
* @example "[email protected]" | ||
* */ | ||
|
||
export const OtpValidation = z.object({ | ||
email: z.string().email(), | ||
}); | ||
|
||
/** | ||
* The email of the user to send OTP. | ||
* @example " | ||
* */ | ||
export const VerifyOtpValidation = z.object({ | ||
email: z.string().email(), | ||
otp: z.string().trim(), | ||
}); | ||
|
||
export type OtpDto = z.infer<typeof OtpValidation>; | ||
export type VerifyOtpDto = z.infer<typeof VerifyOtpValidation>; |
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { GenerateOtpService } from './generateOtp.service'; | ||
import { | ||
OtpDto, | ||
OtpValidation, | ||
VerifyOtpDto, | ||
VerifyOtpValidation, | ||
} from './dto/generateOtp.dto'; | ||
import { Public } from '../auth/decorators/public.decorator'; | ||
|
||
@Controller('otp') | ||
export class GenerateOtpController { | ||
constructor(private readonly generateOtpService: GenerateOtpService) {} | ||
|
||
@Public() | ||
@Post('generate') | ||
async create(@Body() generateOtpDto: OtpDto) { | ||
generateOtpDto = OtpValidation.parse(generateOtpDto); | ||
return await this.generateOtpService.enqueueOtpJob(generateOtpDto); | ||
} | ||
|
||
@Public() | ||
@Post('verify') | ||
async verify(@Body() verifyOtpDto: VerifyOtpDto) { | ||
verifyOtpDto = VerifyOtpValidation.parse(verifyOtpDto); | ||
return (await this.generateOtpService.compareOtp(verifyOtpDto)) | ||
? 'OTP is correct' | ||
: 'OTP is incorrect'; | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GenerateOtpController } from './generateOtp.controller'; | ||
import { GenerateOtpService } from './generateOtp.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [GenerateOtpController], | ||
providers: [GenerateOtpService], | ||
exports: [GenerateOtpService], // not exporting services as no need in testing (??) | ||
}) | ||
export class GenerateOtpModule {} |
Oops, something went wrong.