fix(api-server): fix #3572

This commit is contained in:
JellyBrick
2025-07-13 16:43:29 +09:00
parent e2c849f6c4
commit 33a09cc8e1
2 changed files with 8 additions and 6 deletions

View File

@ -19,7 +19,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
async start(ctx) {
const config = await ctx.getConfig();
await this.init(ctx);
this.init(ctx);
registerCallback((songInfo) => {
this.songInfo = songInfo;
});
@ -60,8 +60,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
},
// Custom
async init(ctx) {
const config = await ctx.getConfig();
init(backendCtx) {
this.app = new Hono();
this.app.use('*', cors());
@ -74,6 +73,8 @@ export const backend = createBackend<BackendType, APIServerConfig>({
// middlewares
this.app.use('/api/*', async (ctx, next) => {
const config = await backendCtx.getConfig();
if (config.authStrategy !== AuthStrategy.NONE) {
return await jwt({
secret: config.secret,
@ -83,6 +84,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
});
this.app.use('/api/*', async (ctx, next) => {
const result = await JWTPayloadSchema.spa(await ctx.get('jwtPayload'));
const config = await backendCtx.getConfig();
const isAuthorized =
config.authStrategy === AuthStrategy.NONE ||
@ -98,12 +100,12 @@ export const backend = createBackend<BackendType, APIServerConfig>({
// routes
registerControl(
this.app,
ctx,
backendCtx,
() => this.songInfo,
() => this.currentRepeatMode,
() => this.volume,
);
registerAuth(this.app, ctx);
registerAuth(this.app, backendCtx);
// swagger
this.app.openAPIRegistry.registerComponent(

View File

@ -15,7 +15,7 @@ export type BackendType = {
currentRepeatMode?: RepeatMode;
volume?: number;
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
init: (ctx: BackendContext<APIServerConfig>) => void;
run: (hostname: string, port: number) => void;
end: () => void;
};