fix(api-server): fix init/authentication error

- Close #2497
This commit is contained in:
JellyBrick
2024-10-14 17:51:59 +09:00
parent 6abcbee290
commit 534aeb163a
6 changed files with 38 additions and 25 deletions

View File

@ -10,14 +10,15 @@ import { createBackend } from '@/utils';
import { JWTPayloadSchema } from './scheme';
import { registerAuth, registerControl } from './routes';
import type { APIServerConfig } from '../config';
import { type APIServerConfig, AuthStrategy } from '../config';
import type { BackendType } from './types';
export const backend = createBackend<BackendType, APIServerConfig>({
async start(ctx) {
const config = await ctx.getConfig();
this.init(ctx);
await this.init(ctx);
registerCallback((songInfo) => {
this.songInfo = songInfo;
});
@ -49,17 +50,20 @@ export const backend = createBackend<BackendType, APIServerConfig>({
this.app.use('*', cors());
// middlewares
this.app.use(
'/api/*',
jwt({
secret: config.secret,
}),
);
this.app.use('/api/*', async (ctx, next) => {
if (config.authStrategy !== AuthStrategy.NONE) {
return await jwt({
secret: config.secret,
})(ctx, next);
}
await next();
});
this.app.use('/api/*', async (ctx, next) => {
const result = await JWTPayloadSchema.spa(await ctx.get('jwtPayload'));
const isAuthorized =
result.success && config.authorizedClients.includes(result.data.id);
config.authStrategy === AuthStrategy.NONE ||
(result.success && config.authorizedClients.includes(result.data.id));
if (!isAuthorized) {
ctx.status(401);
return ctx.body('Unauthorized');