import { Middleware } from "koa";
import dayjs from "dayjs";
import { getMillis } from "taehui-ts/date";

export const validateMillis: Middleware = async (ctx, next) => {
  const {
    headers: { millis },
  } = ctx;

  if (
    dayjs(Number(millis as string))
      .add(1, "minute")
      .isAfter(getMillis())
  ) {
    await next();
  } else {
    ctx.body = { isSilent: true };
    ctx.status = 401;
  }
};
