Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add resend email verification endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 22, 2020
1 parent 141e3a2 commit a7979c2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default () => ({
frontendUrl: process.env.FRONTEND_URL ?? 'http://localhost:3000',
email: {
name: process.env.EMAIL_NAME ?? 'Staart',
from: process.env.EMAIL_FROM,
Expand Down
10 changes: 5 additions & 5 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Body, Controller, Post } from '@nestjs/common';
import { users } from '@prisma/client';
import { RateLimit } from 'nestjs-rate-limiter';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
import { RegisterDto } from './auth.dto';
import { RegisterDto, ResendEmailVerificationDto } from './auth.dto';
import { AuthService } from './auth.service';

@Controller('auth')
Expand All @@ -21,11 +21,11 @@ export class AuthController {

@Post('resend-email-verification')
@RateLimit({
points: 10,
points: 1,
duration: 60,
errorMessage: 'Wait for 60 seconds before trying to create an account',
errorMessage: 'Wait for 60 seconds before requesting another email',
})
async resendVerify() {
return this.authService.resentEmailVerification();
async resendVerify(@Body() data: ResendEmailVerificationDto) {
return this.authService.sendEmailVerification(data.email, true);
}
}
6 changes: 6 additions & 0 deletions src/modules/auth/auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ export class RegisterDto {
@IsOptional()
attributes: Record<string, any>;
}

export class ResendEmailVerificationDto {
@IsEmail()
@IsNotEmpty()
email: string;
}
3 changes: 2 additions & 1 deletion src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { EmailModule } from '../email/email.module';
import { PrismaModule } from '../prisma/prisma.module';
import { UsersService } from '../user/user.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';

@Module({
imports: [PrismaModule, EmailModule],
imports: [PrismaModule, EmailModule, ConfigModule],
controllers: [AuthController],
providers: [AuthService, UsersService],
})
Expand Down
27 changes: 26 additions & 1 deletion src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { users } from '@prisma/client';
import { EmailService } from '../email/email.service';
import { OmitSecrets } from '../prisma/prisma.interface';
Expand All @@ -12,6 +13,7 @@ export class AuthService {
private prisma: PrismaService,
private users: UsersService,
private email: EmailService,
private configService: ConfigService,
) {}

async register(data: RegisterDto): Promise<OmitSecrets<users>> {
Expand All @@ -37,10 +39,33 @@ export class AuthService {
},
},
});
await this.sendEmailVerification(email);
return this.prisma.expose(user);
}

async resentEmailVerification() {
async sendEmailVerification(email: string, resend = false) {
const emailSafe = this.users.getSafeEmail(email);
const emailDetails = await this.prisma.emails.findFirst({
where: { emailSafe },
include: { user: true },
});
if (!emailDetails)
throw new HttpException(
'There is no user for this email',
HttpStatus.NOT_FOUND,
);
this.email.send({
to: `"${emailDetails.user.name}" <${email}>`,
template: resend
? 'auth/resend-email-verification'
: 'auth/email-verification',
data: {
name: emailDetails.user.name,
link: `${this.configService.get<string>(
'frontendUrl',
)}/auth/verify-email?token=`,
},
});
return { queued: true };
}
}

0 comments on commit a7979c2

Please sign in to comment.