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

Commit

Permalink
✨ Add verify emails endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 29, 2020
1 parent f8f47f2 commit 339a29d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ResendEmailVerificationDto,
ResetPasswordDto,
TotpLoginDto,
VerifyEmailDto,
} from './auth.dto';
import { AuthService } from './auth.service';
import { Public } from './public.decorator';
Expand Down Expand Up @@ -82,6 +83,11 @@ export class AuthController {
return this.authService.sendEmailVerification(data.email, true);
}

@Post('verify-email')
async verifyEmail(@Body() data: VerifyEmailDto) {
return this.authService.verifyEmail(data.token);
}

@Post('forgot-password')
@RateLimit({
points: 10,
Expand Down
6 changes: 6 additions & 0 deletions src/modules/auth/auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,9 @@ export class TotpLoginDto {
@IsNotEmpty()
code: string;
}

export class VerifyEmailDto {
@IsString()
@IsNotEmpty()
token: string;
}
15 changes: 12 additions & 3 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { randomStringGenerator } from '@nestjs/common/utils/random-string-genera
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { Authenticator } from '@otplib/core';
import { users } from '@prisma/client';
import { emails, users } from '@prisma/client';
import { compare, hash } from 'bcrypt';
import { authenticator } from 'otplib';
import qrcode from 'qrcode';
Expand All @@ -21,9 +21,9 @@ import { Expose } from '../prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { PwnedService } from '../pwned/pwned.service';
import {
TWO_FACTOR_TOKEN,
PASSWORD_RESET_TOKEN,
EMAIL_VERIFY_TOKEN,
PASSWORD_RESET_TOKEN,
TWO_FACTOR_TOKEN,
} from '../tokens/tokens.constants';
import { TokensService } from '../tokens/tokens.service';
import { RegisterDto } from './auth.dto';
Expand Down Expand Up @@ -283,6 +283,15 @@ export class AuthService {
return this.loginResponse(ipAddress, userAgent, id);
}

async verifyEmail(token: string) {
const id = this.tokensService.verify<number>(EMAIL_VERIFY_TOKEN, token);
const result = await this.prisma.emails.update({
where: { id },
data: { isVerified: true },
});
return this.prisma.expose<emails>(result);
}

private async loginUserWithTotpCode(
ipAddress: string,
userAgent: string,
Expand Down
3 changes: 2 additions & 1 deletion src/modules/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient, sessions, users } from '@prisma/client';
import { emails, PrismaClient, sessions, users } from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';

@Injectable()
Expand All @@ -19,6 +19,7 @@ export class PrismaService extends PrismaClient
delete ((item as any) as users).password;
delete ((item as any) as users).twoFactorSecret;
delete ((item as any) as sessions).token;
delete ((item as any) as emails).emailSafe;
return item;
}
}

0 comments on commit 339a29d

Please sign in to comment.