@@ -10,18 +10,23 @@ import { InjectRepository } from '@nestjs/typeorm';
10
10
import { Cache } from 'cache-manager' ;
11
11
import { Repository } from 'typeorm' ;
12
12
13
- import { User } from '../users/entities/user.entity' ;
14
- import { UserVerification } from './entities/user-verification.entity' ;
13
+ import { User } from '../../ users/entities/user.entity' ;
14
+ import { UserVerification } from '.. /entities/user-verification.entity' ;
15
15
16
- import { VerifyEmailViaOtpInput } from './dto/verify-email-via-otp.input' ;
17
- import { VerifyPhoneViaOtpInput } from './dto/verify-phone-via-otp.input' ;
16
+ import { VerifyEmailViaOtpInput } from '../dto/verify-email-via-otp.input' ;
17
+ import { VerifyPhoneViaOtpInput } from '../dto/verify-phone-via-otp.input' ;
18
+ import { PhoneVerificationService } from '@laze/nestjs-phone-verification' ;
19
+ import { SendOtpInput } from '../dto/send-otp.input' ;
20
+ import { OtpType } from '../enums/otp-type.enum' ;
21
+ import { environment } from '../../../environments/environment' ;
18
22
19
23
@Injectable ( )
20
24
export class VerificationService {
21
25
private readonly logger : Logger ;
22
26
23
27
constructor (
24
28
@Inject ( CACHE_MANAGER ) private readonly cache : Cache ,
29
+ private readonly phoneVerificationService : PhoneVerificationService ,
25
30
@InjectRepository ( UserVerification )
26
31
private readonly userVerificationRepository : Repository < UserVerification > ,
27
32
@InjectRepository ( User )
@@ -72,12 +77,15 @@ export class VerificationService {
72
77
verifyPhoneViaOtpInput : VerifyPhoneViaOtpInput ,
73
78
user : User
74
79
) {
75
- const { code, type } = verifyPhoneViaOtpInput ;
76
- const key = `users.${ user . id } .otp.${ type } ` ;
80
+ const { code } = verifyPhoneViaOtpInput ;
77
81
78
- const otp = await this . cache . get < string > ( key ) ;
82
+ const res = await this . phoneVerificationService . verifyCode ( {
83
+ code,
84
+ number : user . phone ,
85
+ } ) ;
79
86
80
- if ( otp !== code ) {
87
+ if ( ! res . success ) {
88
+ this . logger . error ( res ) ;
81
89
throw new BadRequestException ( 'OTP Code is not valid!' ) ;
82
90
}
83
91
@@ -96,13 +104,35 @@ export class VerificationService {
96
104
97
105
fullUser . verification = verification ;
98
106
99
- await this . cache . del ( key ) ;
100
-
101
107
return fullUser ;
102
108
} catch ( e ) {
103
109
this . logger . error ( e . message , e ) ;
104
110
105
111
throw new BadRequestException ( 'Failed to verify phone via otp!' ) ;
106
112
}
107
113
}
114
+
115
+ async sendOtpForPhoneVerification ( sendOtpInput : SendOtpInput , user : User ) {
116
+ const { type } = sendOtpInput ;
117
+
118
+ const res = await this . phoneVerificationService . sendCode ( {
119
+ number : user . phone ,
120
+ medium : type === OtpType . SMS ? 'sms' : 'voice' ,
121
+ message : `Your ${ environment . app . name } phone number verification code is %otp_code%. Do not share this code with anyone. Visit your ${ environment . app . name } account now to verify.` ,
122
+ } ) ;
123
+
124
+ if ( ! res . success ) {
125
+ this . logger . error ( res ) ;
126
+ throw new BadRequestException (
127
+ 'Failed to send OTP code. Please try again later!'
128
+ ) ;
129
+ }
130
+
131
+ return await this . userRepository . findOneOrFail ( {
132
+ where : {
133
+ id : user . id ,
134
+ } ,
135
+ relations : [ 'profile' , 'verification' ] ,
136
+ } ) ;
137
+ }
108
138
}
0 commit comments