1234567891011121314151617181920212223242526 |
- import { Context, Next } from 'koa';
- import CODE from '@utils/code';
- interface InterfaceSession {
- user?: string
- }
- const noAuths = ['/api/login', '/api/register'];
- export default () => {
- return async (ctx: Context, next: Next): Promise<void> => {
- if (noAuths.includes(ctx.url)) {
- await next();
- return;
- }
- if (ctx.session) {
- const session: InterfaceSession = ctx.session.toJSON();
- if (!session.user) {
- ctx.session.user = null;
- ctx.body = ctx.$response(null, '未登录', false, CODE.NOT_LOGIN);
- return;
- }
- await next();
- } else {
- ctx.status = 500;
- ctx.throw(500, 'session获取失败');
- }
- };
- };
|