Skip to content

Commit 2aef6a9

Browse files
committed
feat: constants
1 parent 665f840 commit 2aef6a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+179
-202
lines changed

src/app.module.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
55
import { MongooseModule } from '@nestjs/mongoose/dist/mongoose.module';
66
import { redisClusterInsStore, redisInsStore } from 'cache-manager-redis-yet';
77

8+
import * as config from 'src/constants';
9+
810
import { AppController } from './app.controller';
911
import { AuthModule } from './auth';
1012
import { CaptchaModule } from './captcha';
@@ -13,9 +15,8 @@ import { EmailModule } from './email';
1315
import { GroupModule } from './group';
1416
import { HelloController } from './hello.controller';
1517
import { IndustryModule } from './industry';
16-
import { config as mongo } from './mongo';
1718
import { NamespaceModule } from './namespace';
18-
import { config as redis, RedisModule } from './redis';
19+
import { RedisModule } from './redis';
1920
import { RegionModule } from './region';
2021
import { RoleModule } from './role';
2122
import { SessionModule } from './session';
@@ -26,9 +27,9 @@ import { UserModule } from './user';
2627
@Module({
2728
imports: [
2829
RedisModule,
29-
MongooseModule.forRoot(mongo.url),
30+
MongooseModule.forRoot(config.mongo.url),
3031
BullModule.forRoot({
31-
redis: redis.url,
32+
redis: config.redis.url,
3233
}),
3334
CacheModule.registerAsync({
3435
isGlobal: true,

src/auth/auth.controller.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
1515

1616
import { JwtPayload } from 'src/auth';
1717
import { CaptchaService } from 'src/captcha';
18+
import { ErrorCodes } from 'src/constants';
19+
import * as config from 'src/constants';
1820
import { addShortTimeSpan } from 'src/lib/lang/time';
19-
import { CreateSessionDto, ErrorCodes as SessionErrorCodes, SessionService } from 'src/session';
21+
import { CreateSessionDto, SessionService } from 'src/session';
2022
import { ThirdPartySource } from 'src/third-party';
21-
import { User, UserDocument, ErrorCodes as UserErrorCodes, UserService } from 'src/user';
23+
import { User, UserDocument, UserService } from 'src/user';
2224

2325
import { AuthService } from './auth.service';
24-
import { ErrorCodes } from './constants';
2526
import { GithubDto } from './dto/github.dto';
2627
import { LoginByEmailDto, LoginByPhoneDto, LoginDto, LogoutDto } from './dto/login.dto';
2728
import { RefreshTokenDto } from './dto/refresh-token.dto';
@@ -343,7 +344,7 @@ export class AuthController {
343344
const user = await this.userService.get(dto.uid);
344345
if (!user) {
345346
throw new NotFoundException({
346-
code: UserErrorCodes.USER_NOT_FOUND,
347+
code: config.ErrorCodes.USER_NOT_FOUND,
347348
message: `user ${dto.uid} not found.`,
348349
});
349350
}
@@ -381,14 +382,14 @@ export class AuthController {
381382
let session = await this.sessionService.findByRefreshToken(dto.refreshToken);
382383
if (!session) {
383384
throw new UnauthorizedException({
384-
code: SessionErrorCodes.SESSION_NOT_FOUND,
385+
code: config.ErrorCodes.SESSION_NOT_FOUND,
385386
message: `session with refresh token ${dto.refreshToken} not found.`,
386387
});
387388
}
388389

389390
if (session.refreshTokenExpireAt.getTime() < Date.now()) {
390391
throw new UnauthorizedException({
391-
code: SessionErrorCodes.SESSION_EXPIRED,
392+
code: config.ErrorCodes.SESSION_EXPIRED,
392393
message: 'Session has been expired.',
393394
});
394395
}

src/auth/auth.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Module } from '@nestjs/common';
44
import { JwtModule } from '@nestjs/jwt';
55

66
import { CaptchaModule } from 'src/captcha';
7+
import * as config from 'src/constants';
78
import { EmailModule } from 'src/email';
89
import { GroupModule } from 'src/group';
910
import { NamespaceModule } from 'src/namespace';
@@ -15,16 +16,15 @@ import { UserModule } from 'src/user';
1516

1617
import { AuthController } from './auth.controller';
1718
import { AuthService } from './auth.service';
18-
import { jwtSecretKey } from './config';
1919

2020
@Module({
2121
imports: [
2222
JwtModule.register({
2323
global: true,
24-
secretOrPrivateKey: jwtSecretKey ?? fs.readFileSync('ssl/private.key', 'utf-8'),
24+
secretOrPrivateKey: config.auth.jwtSecretKey ?? fs.readFileSync('ssl/private.key', 'utf-8'),
2525
signOptions: {
2626
allowInsecureKeySizes: true,
27-
algorithm: jwtSecretKey ? 'HS256' : 'RS256',
27+
algorithm: config.auth.jwtSecretKey ? 'HS256' : 'RS256',
2828
},
2929
}),
3030
UserModule,

src/auth/auth.service.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { Inject, Injectable } from '@nestjs/common';
22
import { RedisClientType } from 'redis';
33

4-
import { createThirdPartyDto } from 'src/third-party/dto/create-third-party.dto';
5-
import { ThirdPartyDoc, ThirdPartySource } from 'src/third-party/entities/third-party.entity';
6-
import { ThirdPartyService } from 'src/third-party/third-party.service';
7-
8-
import * as config from './config';
4+
import * as config from 'src/constants';
95
import {
106
GithubAccessTokenUrl,
117
GithubClientId,
128
GithubClientSecret,
139
GithubUserUrl,
14-
} from './constants';
10+
} from 'src/constants';
11+
import { createThirdPartyDto } from 'src/third-party/dto/create-third-party.dto';
12+
import { ThirdPartyDoc, ThirdPartySource } from 'src/third-party/entities/third-party.entity';
13+
import { ThirdPartyService } from 'src/third-party/third-party.service';
1514

1615
@Injectable()
1716
export class AuthService {
@@ -24,7 +23,7 @@ export class AuthService {
2423
const lock = await this.redisClient.hGetAll(`loginLock:${login}`);
2524
if (!lock || !lock.attempts) return false;
2625

27-
return Number(lock.attempts) >= config.maxLoginAttempts;
26+
return Number(lock.attempts) >= config.auth.maxLoginAttempts;
2827
}
2928

3029
async lock(login: string): Promise<void> {
@@ -38,7 +37,7 @@ export class AuthService {
3837
});
3938

4039
// 设置过期时间为登录锁定时长(秒)
41-
await this.redisClient.expire(lockKey, config.loginLockInS);
40+
await this.redisClient.expire(lockKey, config.auth.loginLockInS);
4241
}
4342

4443
async getGithubAccessToken(code: string): Promise<string> {

src/auth/config.ts

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

src/auth/constants.ts

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

src/auth/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './auth.module';
22
export * from './entities/jwt.entity';
33
export * from './entities/session-with-token.entity';
4-
export * from './constants';

src/captcha/captcha.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import {
2121
} from '@nestjs/swagger';
2222
import { Response } from 'express';
2323

24+
import { ErrorCodes } from 'src/constants';
25+
2426
import { CaptchaService } from './captcha.service';
25-
import { ErrorCodes } from './constants';
2627
import { CreateCaptchaDto } from './dto/create-captcha.dto';
2728
import { ListCaptchasQuery } from './dto/list-captchas.dto';
2829
import { UpdateCaptchaDto } from './dto/update-captcha.dto';

src/captcha/captcha.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import dayjs from 'dayjs';
44
import { DeleteResult } from 'mongodb';
55
import { Model } from 'mongoose';
66

7+
import * as config from 'src/constants';
78
import { buildMongooseQuery } from 'src/mongo';
89

9-
import * as config from './config';
1010
import { CreateCaptchaDto } from './dto/create-captcha.dto';
1111
import { getCaptchaByKeyDto } from './dto/get-captcha.dto';
1212
import { ListCaptchasQuery } from './dto/list-captchas.dto';
@@ -20,7 +20,7 @@ export class CaptchaService {
2020

2121
create(createDto: CreateCaptchaDto) {
2222
if (!createDto.code) {
23-
createDto.code = this.generateCaptcha(config.codeLength);
23+
createDto.code = this.generateCaptcha(config.captcha.codeLength);
2424
}
2525
const createdCaptcha = new this.captchaModel(createDto);
2626
return createdCaptcha.save();
@@ -64,7 +64,7 @@ export class CaptchaService {
6464
{ key },
6565
{
6666
...upsertDto,
67-
expireAt: dayjs().add(config.expiresInS, 'second').toDate(),
67+
expireAt: dayjs().add(config.captcha.expiresInS, 'second').toDate(),
6868
},
6969
{ upsert: true, new: true }
7070
)

src/captcha/config.ts

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

0 commit comments

Comments
 (0)