Files
certd/packages/ui/certd-server/src/controller/user/login/login-controller.ts
T

83 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-11-28 17:36:45 +08:00
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
2025-01-15 01:05:34 +08:00
import { LoginService } from '../../../modules/login/service/login-service.js';
2024-11-28 17:36:45 +08:00
import { BaseController, Constants, SysPublicSettings, SysSettingsService } from '@certd/lib-server';
2025-01-15 01:05:34 +08:00
import { CodeService } from '../../../modules/basic/service/code-service.js';
2024-11-30 01:57:09 +08:00
import { checkComm } from '@certd/plus-core';
/**
*/
@Provide()
@Controller('/api/')
export class LoginController extends BaseController {
@Inject()
loginService: LoginService;
2024-11-28 17:36:45 +08:00
@Inject()
codeService: CodeService;
@Inject()
sysSettingsService: SysSettingsService;
2023-06-27 09:29:43 +08:00
@Post('/login', { summary: Constants.per.guest })
public async login(
@Body(ALL)
2024-10-03 22:03:49 +08:00
user: any
) {
2025-09-11 00:19:38 +08:00
await this.loginService.doCaptchaValidate({form:user})
2024-11-28 17:36:45 +08:00
const token = await this.loginService.loginByPassword(user);
2025-04-17 01:15:55 +08:00
this.writeTokenCookie(token);
return this.ok(token);
}
2025-04-17 01:15:55 +08:00
private writeTokenCookie(token: { expire: any; token: any }) {
this.ctx.cookies.set("certd_token", token.token, {
2025-04-17 01:15:55 +08:00
maxAge: 1000 * token.expire
});
}
@Post('/loginBySms', { summary: Constants.per.guest })
public async loginBySms(
@Body(ALL)
body: any
) {
2024-11-28 17:36:45 +08:00
const settings = await this.sysSettingsService.getSetting<SysPublicSettings>(SysPublicSettings);
if (settings.smsLoginEnabled !== true) {
throw new Error('当前站点禁止短信验证码登录');
}
2024-11-30 01:57:09 +08:00
checkComm();
2024-11-28 17:36:45 +08:00
const token = await this.loginService.loginBySmsCode({
phoneCode: body.phoneCode,
mobile: body.mobile,
smsCode: body.smsCode,
randomStr: body.randomStr,
});
2025-04-17 01:15:55 +08:00
this.writeTokenCookie(token);
return this.ok(token);
}
@Post('/loginByTwoFactor', { summary: Constants.per.guest })
public async loginByTwoFactor(
@Body(ALL)
body: any
) {
const token = await this.loginService.loginByTwoFactor({
2025-04-17 22:34:21 +08:00
loginId: body.loginId,
2025-04-17 01:15:55 +08:00
verifyCode: body.verifyCode,
});
2025-04-17 01:15:55 +08:00
this.writeTokenCookie(token);
return this.ok(token);
}
2023-06-27 09:29:43 +08:00
@Post('/logout', { summary: Constants.per.authOnly })
public logout() {
this.ctx.cookies.set("certd_token", "", {
maxAge: 0
});
return this.ok();
}
}