Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.

Commit 81ad3ac

Browse files
committed
feat: use nestjs-redis and ioredis instead of redis-om
1 parent b33df0b commit 81ad3ac

File tree

10 files changed

+78
-226
lines changed

10 files changed

+78
-226
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,15 @@
3131
"prisma": {
3232
"schema": "prisma/schema.prisma"
3333
},
34+
"engines": {
35+
"node": ">=18"
36+
},
37+
"pnpm": {
38+
"overrides": {
39+
"@liaoliaots/nestjs-redis>@nestjs/common": "^10.0.0",
40+
"@liaoliaots/nestjs-redis>@nestjs/core": "^10.0.0",
41+
"@liaoliaots/nestjs-redis>@nestjs/terminus": "^10.0.0"
42+
}
43+
},
3444
"packageManager": "[email protected]"
3545
}

packages/backend/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"test:e2e": "jest --config test/jest-e2e.json"
1919
},
2020
"dependencies": {
21+
"@liaoliaots/nestjs-redis": "^9.0.5",
2122
"@nestjs/common": "^10.0.0",
2223
"@nestjs/config": "^3.0.0",
2324
"@nestjs/core": "^10.0.0",
@@ -35,8 +36,6 @@
3536
"mint-filter": "^4.0.3",
3637
"nest-router": "^1.0.9",
3738
"openai": "^4.14.0",
38-
"redis": "^4.6.7",
39-
"redis-om": "^0.4.2",
4039
"reflect-metadata": "^0.1.13",
4140
"rxjs": "^7.8.1",
4241
"spark-md5": "^3.0.2"

packages/backend/src/app.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RedisModule } from '@liaoliaots/nestjs-redis';
12
import { Module } from '@nestjs/common';
23
import { ConfigModule } from '@nestjs/config';
34
import { APP_GUARD } from '@nestjs/core';
@@ -22,6 +23,11 @@ import configuration from './configuration';
2223
load: [configuration],
2324
isGlobal: true,
2425
}),
26+
RedisModule.forRoot({
27+
config: {
28+
url: configuration().redis.url,
29+
},
30+
}),
2531
AuthModule,
2632
UserModule,
2733
ChatModule,

packages/backend/src/modules/auth/auth.module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import { JwtModule } from '@/libs/jwt/jwt.module';
55
import { SmsModule } from '@/libs/sms/sms.module';
66
import { WechatService } from '@/modules/auth/wechat.service';
77
import { DatabaseService } from '@/processors/database/database.service';
8-
import { RedisModule } from '@/processors/redis/redis.module';
98

109
import { AuthController } from './auth.controller';
1110
import { AuthService } from './auth.service';
1211

1312
@Module({
14-
imports: [JwtModule, RedisModule, EmailModule, SmsModule],
13+
imports: [JwtModule, EmailModule, SmsModule],
1514
controllers: [AuthController],
1615
providers: [AuthService, DatabaseService, WechatService],
1716
})

packages/backend/src/modules/auth/auth.service.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { compare, hashSync } from 'bcrypt';
2+
import { Redis } from 'ioredis';
23
import * as Joi from 'joi';
34

5+
import { InjectRedis, RedisService } from '@liaoliaots/nestjs-redis';
46
import { Injectable } from '@nestjs/common';
57
import { Role } from '@prisma/client';
68

@@ -9,7 +11,6 @@ import { EmailService } from '@/libs/email/email.service';
911
import { JwtService } from '@/libs/jwt/jwt.service';
1012
import { SmsService } from '@/libs/sms/sms.service';
1113
import { DatabaseService } from '@/processors/database/database.service';
12-
import { RedisService } from '@/processors/redis/redis.service';
1314

1415
import { IAccountStatus } from 'shared';
1516
import { 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()
4350
export 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
/* 绑定用户身份 */

packages/backend/src/processors/redis/auth-code.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/backend/src/processors/redis/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/backend/src/processors/redis/redis.module.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/backend/src/processors/redis/redis.service.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)