-
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.
feat: email confirmation + otp backup codes
- Loading branch information
1 parent
1a3bfab
commit 38e730b
Showing
16 changed files
with
2,165 additions
and
136 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
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,14 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsEmail } from 'class-validator'; | ||
import { IsNotEmptyString } from '../../common/decorators/is-not-empty-string.decorator'; | ||
|
||
export class ConfirmEmailDto { | ||
@ApiProperty({ description: 'User email', example: 'email@mysite.com' }) | ||
@IsEmail() | ||
@IsNotEmptyString() | ||
readonly email!: string; | ||
|
||
@ApiProperty({ description: 'User code', example: '123456' }) | ||
@IsNotEmptyString() | ||
readonly code!: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface I2FAResponse { | ||
twoFactorToken: string; | ||
} | ||
|
||
export interface ILoginResponse { | ||
accessToken: string; | ||
refreshToken: string; | ||
} | ||
|
||
export interface I2FaEnabled { | ||
otpCodes: 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum UserStatus { | ||
ACTIVE = 'active', | ||
PENDING = 'pending', | ||
BANNED = 'banned', | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/api-gateway/src/common/modules/mailer/mailer.module.ts
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,34 @@ | ||
import { MailerModule } from '@nestjs-modules/mailer'; | ||
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter'; | ||
import { Module } from '@nestjs/common'; | ||
import { MailService } from './mailer.service'; | ||
import { join } from 'path'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
MailerModule.forRootAsync({ | ||
imports: [ConfigModule], | ||
useFactory: async (config: ConfigService) => { | ||
const transport = config.get<string>('email.transport'); | ||
return { | ||
transport: transport ?? { jsonTransport: true }, | ||
defaults: { | ||
from: config.get<string>('email.from'), | ||
}, | ||
template: { | ||
dir: join(__dirname, 'templates'), | ||
adapter: new HandlebarsAdapter(), | ||
options: { | ||
strict: true, | ||
}, | ||
}, | ||
}; | ||
}, | ||
inject: [ConfigService], | ||
}), | ||
], | ||
providers: [MailService], | ||
}) | ||
export class MailModule {} |
25 changes: 25 additions & 0 deletions
25
packages/api-gateway/src/common/modules/mailer/mailer.service.ts
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,25 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { OnEvent } from '@nestjs/event-emitter'; | ||
import { EmailConfirmation, EmailConfirmationDto } from '../../../events'; | ||
import { MailerService } from '@nestjs-modules/mailer'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class MailService { | ||
appName: string; | ||
constructor( | ||
private readonly mail: MailerService, | ||
private readonly config: ConfigService, | ||
) { | ||
this.appName = this.config.get<string>('app.name') as string; | ||
} | ||
@OnEvent(EmailConfirmation) | ||
async emailConfirmation({ email, code }: EmailConfirmationDto) { | ||
await this.mail.sendMail({ | ||
to: email, | ||
subject: `Welcome to ${this.appName}! Confirm your Email`, | ||
template: 'email-confirmation', | ||
context: { code, appName: this.appName }, | ||
}); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/api-gateway/src/common/modules/mailer/templates/email-confirmation.hbs
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,6 @@ | ||
<h3>Welcome to {{appName}}!</h3> | ||
<p>Your email authentication code is {{code}} - Valid for 8 minutes.</p> | ||
<p>For account security purposes, please do not share this authentication code | ||
with anyone.</p> | ||
<p>Regards,</p> | ||
<p>{{appName}} Team</p> |
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,5 @@ | ||
export const EmailConfirmation = 'email.confirmation.token'; | ||
export interface EmailConfirmationDto { | ||
email: string; | ||
code: string; | ||
} |
Oops, something went wrong.