11import { compare , hashSync } from 'bcrypt' ;
2+ import { Redis } from 'ioredis' ;
23import * as Joi from 'joi' ;
34
5+ import { InjectRedis , RedisService } from '@liaoliaots/nestjs-redis' ;
46import { Injectable } from '@nestjs/common' ;
57import { Role } from '@prisma/client' ;
68
@@ -9,7 +11,6 @@ import { EmailService } from '@/libs/email/email.service';
911import { JwtService } from '@/libs/jwt/jwt.service' ;
1012import { SmsService } from '@/libs/sms/sms.service' ;
1113import { DatabaseService } from '@/processors/database/database.service' ;
12- import { RedisService } from '@/processors/redis/redis.service' ;
1314
1415import { IAccountStatus } from 'shared' ;
1516import { ErrorCodeEnum } from 'shared/dist/error-code' ;
@@ -39,9 +40,16 @@ const getPhoneOrEmail = (identity: string) => {
3940 }
4041} ;
4142
43+ function generateRandomSixDigitNumber ( ) {
44+ const min = 100000 ;
45+ const max = 999999 ;
46+ return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
47+ }
48+
4249@Injectable ( )
4350export class AuthService {
4451 constructor (
52+ @InjectRedis ( ) private readonly redis : Redis ,
4553 private jwt : JwtService ,
4654 private prisma : DatabaseService ,
4755 private redisService : RedisService ,
@@ -53,7 +61,7 @@ export class AuthService {
5361 * 1. 检查是否绑定账户
5462 * 2. 检查是否设置密码
5563 */
56- async _signWithCheck ( user : any ) : Promise < {
64+ async #signWithCheck ( user : any ) : Promise < {
5765 token : string ;
5866 status : IAccountStatus ;
5967 } > {
@@ -69,6 +77,16 @@ export class AuthService {
6977 } ;
7078 }
7179
80+ async #verifyCode( identity : string , code : string ) {
81+ const isValid = ( await this . redis . get ( identity ) ) === code ;
82+
83+ if ( ! isValid ) {
84+ throw new BizException ( ErrorCodeEnum . CodeValidationError ) ;
85+ } else {
86+ await this . redis . del ( identity ) ;
87+ }
88+ }
89+
7290 /* 添加验证码 */
7391 async newValidateCode ( identity : string ) {
7492 const { email, phone } = getPhoneOrEmail ( identity ) ;
@@ -77,24 +95,26 @@ export class AuthService {
7795 success : false ,
7896 } ;
7997 }
98+ const ttl = await this . redis . ttl ( identity ) ;
99+ /* if key not exist, ttl will be -2 */
100+ if ( 600 - ttl < 60 ) {
101+ return {
102+ success : false ,
103+ ttl,
104+ } ;
105+ } else {
106+ const newTtl = 10 * 60 ;
107+ const code = generateRandomSixDigitNumber ( ) ;
108+ await this . redis . setex ( identity , newTtl , code ) ;
80109
81- /* 10分钟内仅可发送一次 */
82- const code = await this . redisService . authCode . new ( identity ) ;
83-
84- if ( code . success ) {
85110 if ( email ) {
86- await this . emailService . sendCode ( identity , code . code ) ;
111+ await this . emailService . sendCode ( identity , code ) ;
87112 } else if ( phone ) {
88- await this . smsService . sendCode ( identity , code . code ) ;
113+ await this . smsService . sendCode ( identity , code ) ;
89114 }
90115 return {
91116 success : true ,
92- ttl : code . ttl ,
93- } ;
94- } else {
95- return {
96- success : false ,
97- ttl : code . ttl ,
117+ ttl : newTtl ,
98118 } ;
99119 }
100120 }
@@ -103,11 +123,7 @@ export class AuthService {
103123 async WithValidateCode ( identity : string , code : string ) {
104124 const { email, phone } = getPhoneOrEmail ( identity ) ;
105125
106- const isValid = await this . redisService . authCode . valid ( identity , code ) ;
107-
108- if ( ! isValid ) {
109- throw new BizException ( ErrorCodeEnum . CodeValidationError ) ;
110- }
126+ await this . #verifyCode( identity , code ) ;
111127
112128 const existUser = await this . prisma . user . findMany ( {
113129 where : {
@@ -128,7 +144,7 @@ export class AuthService {
128144 } else {
129145 user = existUser [ 0 ] ;
130146 }
131- return this . _signWithCheck ( user ) ;
147+ return this . #signWithCheck ( user ) ;
132148 }
133149
134150 /* 通过密码登录 */
@@ -147,7 +163,7 @@ export class AuthService {
147163 if ( ! isPasswordCorrect ) {
148164 throw Error ( 'Password is incorrect' ) ;
149165 }
150- return this . _signWithCheck ( user [ 0 ] ) ;
166+ return this . #signWithCheck ( user [ 0 ] ) ;
151167 }
152168
153169 /* 添加密码 */
@@ -186,11 +202,7 @@ export class AuthService {
186202 async forgetPassword ( identity : string , code : string , password : string ) {
187203 const { email, phone } = getPhoneOrEmail ( identity ) ;
188204
189- const isValid = await this . redisService . authCode . valid ( identity , code ) ;
190-
191- if ( ! isValid ) {
192- throw new BizException ( ErrorCodeEnum . CodeValidationError ) ;
193- }
205+ await this . #verifyCode( identity , code ) ;
194206
195207 const existUser = await this . prisma . user . findMany ( {
196208 where : {
@@ -206,7 +218,7 @@ export class AuthService {
206218 }
207219 await this . changePassword ( user . id , password ) ;
208220
209- return this . _signWithCheck ( user ) ;
221+ return this . #signWithCheck ( user ) ;
210222 }
211223
212224 /* 绑定用户身份 */
0 commit comments