auth.ts 747 B

12345678910111213141516171819202122232425262728
  1. import { getClientConfig } from "@/app/config/client";
  2. import { ACCESS_CODE_PREFIX } from "@/app/constant";
  3. import { useAccessStore } from "@/app/store";
  4. export function bearer(value: string) {
  5. return `Bearer ${value.trim()}`;
  6. }
  7. export function getAuthHeaders(apiKey = "") {
  8. const accessStore = useAccessStore.getState();
  9. const isApp = !!getClientConfig()?.isApp;
  10. let headers: Record<string, string> = {};
  11. if (apiKey) {
  12. // use user's api key first
  13. headers.Authorization = bearer(apiKey);
  14. } else if (
  15. accessStore.enabledAccessControl() &&
  16. !isApp &&
  17. !!accessStore.accessCode
  18. ) {
  19. // or use access code
  20. headers.Authorization = bearer(ACCESS_CODE_PREFIX + accessStore.accessCode);
  21. }
  22. return headers;
  23. }