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

107 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-09-11 23:47:05 +08:00
import { ALL, Body, Controller, Inject, Post, Provide } from "@midwayjs/core";
import { LoginService } from "../../../modules/login/service/login-service.js";
import { AddonService, BaseController, Constants, SysPublicSettings, SysSettingsService } from "@certd/lib-server";
import { CodeService } from "../../../modules/basic/service/code-service.js";
import { checkComm } from "@certd/plus-core";
import { logger } from "@certd/basic";
import { ICaptchaAddon } from "../../../plugins/plugin-captcha/api.js";
/**
*/
@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;
2025-09-11 23:47:05 +08:00
@Inject()
addonService: AddonService;
2024-11-28 17:36:45 +08:00
2023-06-27 09:29:43 +08:00
@Post('/login', { summary: Constants.per.guest })
public async login(
@Body(ALL)
2025-09-11 23:47:05 +08:00
body: any
) {
2025-09-11 23:47:05 +08:00
await this.loginService.doCaptchaValidate({form:body.captcha})
const token = await this.loginService.loginByPassword(body);
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();
}
2025-09-11 23:47:05 +08:00
@Post('/captcha/getParams', { summary: Constants.per.guest })
async getCaptchaParams() {
const settings = await this.sysSettingsService.getPublicSettings()
if (settings.captchaEnabled) {
const addonId = settings.captchaAddonId;
const addon:ICaptchaAddon = await this.addonService.getAddonById(addonId,true,0)
if (!addon) {
logger.warn('验证码插件还未配置')
return this.ok({});
}
const params = await addon.getClientParams()
return this.ok(params);
}
return this.ok({});
}
}