feat: 认证相关
This commit is contained in:
23
apps/server/middlewares/authorization.ts
Normal file
23
apps/server/middlewares/authorization.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type Koa from 'koa';
|
||||
import type { Middleware } from './types.ts';
|
||||
|
||||
export class Authorization implements Middleware {
|
||||
private readonly ignoreAuth = [
|
||||
'/api/auth/login',
|
||||
'/api/auth/info',
|
||||
'/api/auth/url',
|
||||
];
|
||||
|
||||
apply(app: Koa) {
|
||||
app.use(async (ctx: Koa.Context, next: Koa.Next) => {
|
||||
console.log('ctx.path', ctx.path)
|
||||
if (this.ignoreAuth.includes(ctx.path)) {
|
||||
return next();
|
||||
}
|
||||
if (ctx.session.isNew) {
|
||||
ctx.throw(401, 'Unauthorized');
|
||||
}
|
||||
await next();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user