11import {
22 BadRequestException ,
3+ ConflictException ,
34 Injectable ,
45 NotFoundException ,
56} from '@nestjs/common' ;
@@ -9,84 +10,62 @@ import { EmailService } from 'src/email/email.service';
910import { CryptoService } from './crypto.service' ;
1011import { LoginRequestData } from './dto/login-user-dto' ;
1112import * as bcrypt from 'bcrypt' ;
12- import { MoreThan , Repository } from 'typeorm' ;
13+ import { LessThan , Repository } from 'typeorm' ;
1314import { InjectRepository } from '@nestjs/typeorm' ;
1415import { EmailVerification } from './email-verification.entity' ;
1516import { EmailVerificationPayload } from './interface' ;
16- import { EmailMessages } from './messages' ;
1717import { JwtPayload } from 'src/types/JwtPayload' ;
18- import { ResendVerificationRequestData } from './dto/resend-verification-request-data ' ;
19- import { VerifyTokenRequestData } from './dto/verify-token-request-data ' ;
18+ import { EmailMessages } from '../config/messages ' ;
19+ import { ResendEmailVerificationRequestData } from './dto/resend-verification-dto ' ;
2020import { SendMailRequestData } from '../email/dto/send-mail-request-data' ;
21- import { EventEmitter2 , OnEvent } from '@nestjs/event-emitter' ;
22- import { SendVerificationMailEvent } from 'src/event/send-verification-mail.event' ;
2321import { UserService } from '../user/user.service' ;
22+ import { VerifyTokenRequestData } from './dto/verify-token-request-data' ;
2423
2524@Injectable ( )
2625export class AuthService {
2726 constructor (
28- private readonly userService : UserService ,
2927 private readonly jwtService : JwtService ,
3028 private readonly emailService : EmailService ,
29+ private readonly userService : UserService ,
3130 @InjectRepository ( EmailVerification )
3231 private readonly emailVerificationRepo : Repository < EmailVerification > ,
3332 private readonly cryptoService : CryptoService ,
34- private readonly eventEmitter : EventEmitter2 ,
3533 ) { }
3634
3735 async signUp ( signUpUserDto : SignupRequestData ) : Promise < { message : string } > {
38- const existingUser = await this . userService . findByUserNameAndEmail (
39- signUpUserDto . username ,
40- signUpUserDto . email ,
41- ) ;
42-
43- if ( existingUser ) {
44- if ( existingUser . username === signUpUserDto . username ) {
45- throw new BadRequestException ( EmailMessages . usernameTaken ) ;
46- }
47-
48- if ( existingUser . email === signUpUserDto . email ) {
49- throw new BadRequestException ( EmailMessages . emailTaken ) ;
50- }
51- }
52-
5336 const hashedPassword = await this . cryptoService . hashPassword (
5437 signUpUserDto . password ,
5538 ) ;
5639
57- const { email, fullName, username } = signUpUserDto ;
58-
59- await this . userService . create ( {
40+ const createUserRequestData : SignupRequestData = {
41+ ...signUpUserDto ,
6042 password : hashedPassword ,
61- email,
62- fullName,
63- username,
64- } ) ;
65-
66- const resendVerificationRequestData : ResendVerificationRequestData = {
67- email : email ,
6843 } ;
6944
70- const event = new SendVerificationMailEvent (
71- resendVerificationRequestData . email ,
72- ) ;
73- this . eventEmitter . emit ( 'send.mail' , event ) ;
45+ const user = await this . userService . create ( createUserRequestData ) ;
7446
75- return { message : 'Sign up succesfull. Verification mail has been sent.' } ;
47+ const requestData : ResendEmailVerificationRequestData = {
48+ email : user . email ,
49+ } ;
50+ await this . sendVerificationLink ( requestData ) ;
51+
52+ return {
53+ message :
54+ 'Sign Up successful. Verification mail has been sent. Please verify' ,
55+ } ;
7656 }
7757
78- @OnEvent ( 'send.mail' )
7958 async sendVerificationLink (
80- resendVerificationRequestData : ResendVerificationRequestData ,
59+ resendEmailVerificationRequestData : ResendEmailVerificationRequestData ,
8160 ) {
82- const email = resendVerificationRequestData . email ;
61+ const email = resendEmailVerificationRequestData . email ;
8362 const user = await this . userService . findOneByField ( 'email' , email ) ;
8463 if ( ! user ) {
85- throw new BadRequestException ( 'User not found' ) ;
64+ throw new NotFoundException ( 'User not found' ) ;
8665 }
8766
88- if ( user . verifiedAt !== null ) {
89- throw new BadRequestException ( 'User is already verified' ) ;
67+ if ( user . verifiedAt ) {
68+ throw new ConflictException ( 'User is already verified' ) ;
9069 }
9170
9271 await this . emailVerificationRepo . softDelete ( {
@@ -128,22 +107,19 @@ export class AuthService {
128107 message : EmailMessages . emailSendSuccess ,
129108 } ;
130109 }
110+
131111 async verify ( verifyTokenRequestData : VerifyTokenRequestData ) {
132112 const token = verifyTokenRequestData . token ;
133-
134113 const payload = this . jwtService . verify < EmailVerificationPayload > ( token , {
135114 secret : process . env . JWT_VERIFICATION_TOKEN_SECRET ,
136115 } ) ;
137116
138117 const record = await this . emailVerificationRepo . findOne ( {
139- where : {
140- token,
141- expiresAt : MoreThan ( new Date ( ) ) ,
142- } ,
118+ where : { token, expiresAt : LessThan ( new Date ( ) ) } ,
143119 } ) ;
144120
145121 if ( ! record ) {
146- throw new BadRequestException ( 'Token has expired' ) ;
122+ throw new NotFoundException ( 'Token not found or has expired' ) ;
147123 }
148124
149125 await this . emailVerificationRepo . save ( record ) ;
@@ -172,7 +148,7 @@ export class AuthService {
172148 throw new NotFoundException ( 'User not found' ) ;
173149 }
174150
175- if ( user . verifiedAt === null ) {
151+ if ( ! user . verifiedAt ) {
176152 throw new BadRequestException (
177153 'User not verified. Please verify before login' ,
178154 ) ;
@@ -192,9 +168,9 @@ export class AuthService {
192168 return { accessToken : await this . jwtService . signAsync ( payload ) } ;
193169 }
194170
195- async validateToken (
196- verifyTokenRequestData : VerifyTokenRequestData ,
197- ) : Promise < JwtPayload > {
171+ async validateToken ( verifyTokenRequestData : {
172+ token : string ;
173+ } ) : Promise < JwtPayload > {
198174 const decoded = await this . jwtService . verifyAsync < JwtPayload > (
199175 verifyTokenRequestData . token ,
200176 {
0 commit comments