chore: passkey登录优化

This commit is contained in:
xiaojunnuo
2026-03-13 15:31:03 +08:00
parent 12fed34e10
commit eae4f721e8
11 changed files with 136 additions and 109 deletions
@@ -4,6 +4,7 @@ import { AddonService, BaseController, Constants, SysPublicSettings, SysSettings
import { CodeService } from "../../../modules/basic/service/code-service.js";
import { checkComm } from "@certd/plus-core";
import { CaptchaService } from "../../../modules/basic/service/captcha-service.js";
import { PasskeyService } from "../../../modules/login/service/passkey-service.js";
/**
*/
@@ -23,6 +24,10 @@ export class LoginController extends BaseController {
@Inject()
captchaService: CaptchaService;
@Inject()
passkeyService: PasskeyService;
@Post('/login', { summary: Constants.per.guest })
public async login(
@Body(ALL)
@@ -81,22 +86,36 @@ export class LoginController extends BaseController {
return this.ok(token);
}
@Post('/loginByPasskey', { summary: Constants.per.guest })
public async loginByPasskey(
@Body(ALL)
body: any
) {
const credential = body.credential;
const challenge = body.challenge;
const token = await this.loginService.loginByPasskey({
credential,
challenge,
}, this.ctx);
// this.writeTokenCookie(token);
return this.ok(token);
}
@Post('/passkey/generateAuthentication', { summary: Constants.per.guest })
public async generateAuthentication() {
const options = await this.passkeyService.generateAuthenticationOptions(
this.ctx
);
return this.ok({
...options,
});
}
@Post('/loginByPasskey', { summary: Constants.per.guest })
public async loginByPasskey(
@Body(ALL)
body: any
) {
const credential = body.credential;
const challenge = body.challenge;
const token = await this.loginService.loginByPasskey({
credential,
challenge,
}, this.ctx);
// this.writeTokenCookie(token);
return this.ok(token);
}
@Post('/logout', { summary: Constants.per.authOnly })
public logout() {
@@ -1,99 +0,0 @@
import { ALL, Body, Controller, Inject, Post, Provide, RequestIP } from "@midwayjs/core";
import { PasskeyService } from "../../../modules/login/service/passkey-service.js";
import { BaseController, Constants } from "@certd/lib-server";
import { UserService } from "../../../modules/sys/authority/service/user-service.js";
@Provide()
@Controller('/api/passkey')
export class PasskeyController extends BaseController {
@Inject()
passkeyService: PasskeyService;
@Inject()
userService: UserService;
@Post('/generateRegistration', { summary: Constants.per.authOnly })
public async generateRegistration(
@Body(ALL)
body: any,
@RequestIP()
remoteIp: string
) {
const userId = this.getUserId()
const user = await this.userService.info(userId);
if (!user) {
throw new Error('用户不存在');
}
const options = await this.passkeyService.generateRegistrationOptions(
userId,
user.username,
remoteIp,
this.ctx
);
return this.ok({
...options,
userId
});
}
@Post('/verifyRegistration', { summary: Constants.per.guest })
public async verifyRegistration(
@Body(ALL)
body: any
) {
const userId = body.userId;
const response = body.response;
const challenge = body.challenge;
const deviceName = body.deviceName;
const result = await this.passkeyService.registerPasskey(
userId,
response,
challenge,
deviceName,
this.ctx
);
return this.ok(result);
}
@Post('/generateAuthentication', { summary: Constants.per.guest })
public async generateAuthentication(
@Body(ALL)
body: any
) {
const options = await this.passkeyService.generateAuthenticationOptions(
this.ctx
);
return this.ok({
...options,
});
}
@Post('/register', { summary: Constants.per.guest })
public async registerPasskey(
@Body(ALL)
body: any
) {
const userId = body.userId;
const response = body.response;
const deviceName = body.deviceName;
const challenge = body.challenge;
const result = await this.passkeyService.registerPasskey(
userId,
response,
challenge,
deviceName,
this.ctx
);
return this.ok(result);
}
}