Skip to content

Commit

Permalink
fix: 修复jwt升级后,build报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
luch1994 committed Feb 5, 2025
1 parent 4a6dbf9 commit df0e153
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"minio": "^7.1.3",
"moment": "^2.30.1",
"mongodb": "^5.9.2",
"ms": "^2.1.3",
"nanoid": "^3.3.7",
"node-fetch": "^2.7.0",
"node-forge": "^1.3.1",
Expand Down
32 changes: 8 additions & 24 deletions server/src/modules/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,10 @@ export class AuthController {
password: userInfo.password,
});

const token = await this.authService.generateToken(
{
username: user.username,
_id: user._id.toString(),
},
{
secret: this.configService.get<string>('XIAOJU_SURVEY_JWT_SECRET'),
expiresIn: this.configService.get<string>(
'XIAOJU_SURVEY_JWT_EXPIRES_IN',
),
},
);
const token = await this.authService.generateToken({
username: user.username,
_id: user._id.toString(),
});
// 验证过的验证码要删掉,防止被别人保存重复调用
this.captchaService.deleteCaptcha(userInfo.captchaId);
return {
Expand Down Expand Up @@ -136,18 +128,10 @@ export class AuthController {
}
let token;
try {
token = await this.authService.generateToken(
{
username: user.username,
_id: user._id.toString(),
},
{
secret: this.configService.get<string>('XIAOJU_SURVEY_JWT_SECRET'),
expiresIn: this.configService.get<string>(
'XIAOJU_SURVEY_JWT_EXPIRES_IN',
),
},
);
token = await this.authService.generateToken({
username: user.username,
_id: user._id.toString(),
});
// 验证过的验证码要删掉,防止被别人保存重复调用
this.captchaService.deleteCaptcha(userInfo.captchaId);
} catch (error) {
Expand Down
17 changes: 10 additions & 7 deletions server/src/modules/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import { sign, verify } from 'jsonwebtoken';
import { sign, verify, SignOptions } from 'jsonwebtoken';
import { StringValue } from 'ms';
import { UserService } from './user.service';

@Injectable()
Expand All @@ -10,13 +11,15 @@ export class AuthService {
private readonly configService: ConfigService,
private readonly userService: UserService,
) {}
async generateToken(
{ _id, username }: { _id: string; username: string },
{ secret, expiresIn }: { secret: string; expiresIn: string },
) {
return sign({ _id, username }, secret, {
async generateToken({ _id, username }: { _id: string; username: string }) {
const secret = this.configService.get<string>('XIAOJU_SURVEY_JWT_SECRET');
const expiresIn: StringValue = this.configService.get<StringValue>(
'XIAOJU_SURVEY_JWT_EXPIRES_IN',
);
const signOptions: SignOptions = {
expiresIn,
});
};
return sign({ _id, username }, secret, signOptions);
}

async verifyToken(token: string) {
Expand Down

0 comments on commit df0e153

Please sign in to comment.