修改登陆逻辑

This commit is contained in:
whyour 2022-10-29 10:05:54 +08:00
parent 5e16ace1e7
commit 3e0553c3e8

View File

@ -24,7 +24,7 @@ export default class UserService {
@Inject('logger') private logger: winston.Logger, @Inject('logger') private logger: winston.Logger,
private scheduleService: ScheduleService, private scheduleService: ScheduleService,
private sockService: SockService, private sockService: SockService,
) { } ) {}
public async login( public async login(
payloads: { payloads: {
@ -67,7 +67,9 @@ export default class UserService {
const retriesTime = Math.pow(3, retries) * 1000; const retriesTime = Math.pow(3, retries) * 1000;
if (retries > 2 && timestamp - lastlogon < retriesTime) { if (retries > 2 && timestamp - lastlogon < retriesTime) {
const waitTime = Math.ceil((retriesTime - (timestamp - lastlogon)) / 1000); const waitTime = Math.ceil(
(retriesTime - (timestamp - lastlogon)) / 1000,
);
return { return {
code: 410, code: 410,
message: `失败次数过多,请${waitTime}秒后重试`, message: `失败次数过多,请${waitTime}秒后重试`,
@ -75,9 +77,12 @@ export default class UserService {
}; };
} }
const { ip, address } = await getNetIp(req); if (
if (username === cUsername && password === cPassword) { username === cUsername &&
if (twoFactorActivated && needTwoFactor) { password === cPassword &&
twoFactorActivated &&
needTwoFactor
) {
this.updateAuthInfo(content, { this.updateAuthInfo(content, {
isTwoFactorChecking: true, isTwoFactorChecking: true,
}); });
@ -87,6 +92,8 @@ export default class UserService {
}; };
} }
const { ip, address } = await getNetIp(req);
if (username === cUsername && password === cPassword) {
const data = createRandomString(50, 100); const data = createRandomString(50, 100);
const expiration = twoFactorActivated ? 60 : 20; const expiration = twoFactorActivated ? 60 : 20;
let token = jwt.sign({ data }, config.secret as any, { let token = jwt.sign({ data }, config.secret as any, {
@ -109,7 +116,8 @@ export default class UserService {
}); });
await this.notificationService.notify( await this.notificationService.notify(
'登录通知', '登录通知',
`你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}${address} ${req.platform `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}${address} ${
req.platform
} ip地址 ${ip}`, } ip地址 ${ip}`,
); );
await this.getLoginLog(); await this.getLoginLog();
@ -137,7 +145,8 @@ export default class UserService {
}); });
await this.notificationService.notify( await this.notificationService.notify(
'登录通知', '登录通知',
`你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}${address} ${req.platform `你于${dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}${address} ${
req.platform
} ip地址 ${ip}`, } ip地址 ${ip}`,
); );
await this.getLoginLog(); await this.getLoginLog();