me.get.mjs 1.52 KB
import { e as eventHandler, g as getHeader, b as getMockUserByToken } from '../../../nitro/nitro.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:url';
import '@iconify/utils';
import 'consola';

function getTokenFromAuthorizationHeader(value) {
  if (!value) {
    return null;
  }
  const [type, token] = value.split(" ");
  if ((type == null ? void 0 : type.toLowerCase()) !== "bearer" || !token) {
    return null;
  }
  return token;
}
const me_get = eventHandler((event) => {
  const authorization = getHeader(event, "authorization");
  const token = getTokenFromAuthorizationHeader(authorization);
  if (!token) {
    return {
      success: false,
      state: "failed",
      nextAction: "relogin",
      errorCode: "TOKEN_MISSING",
      message: "\u672A\u63D0\u4F9B\u6709\u6548 token\u3002"
    };
  }
  const result = getMockUserByToken(token);
  if (!result.success) {
    return {
      success: false,
      state: "failed",
      nextAction: "relogin",
      errorCode: result.errorCode,
      message: result.errorCode === "TOKEN_EXPIRED" ? "token \u5DF2\u8FC7\u671F\u3002" : "token \u65E0\u6548\u3002"
    };
  }
  return {
    success: true,
    state: "authenticated",
    nextAction: "continue",
    errorCode: null,
    message: "token \u6821\u9A8C\u901A\u8FC7\u3002",
    user: result.user,
    expiresAt: new Date(result.expiresAt).toISOString()
  };
});

export { me_get as default };
//# sourceMappingURL=me.get.mjs.map