auth.ts 690 B

123456789101112131415161718192021222324252627
  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 getAuthKey(apiKey = "") {
  8. const accessStore = useAccessStore.getState();
  9. const isApp = !!getClientConfig()?.isApp;
  10. let authKey = "";
  11. if (apiKey) {
  12. // use user's api key first
  13. authKey = bearer(apiKey);
  14. } else if (
  15. accessStore.enabledAccessControl() &&
  16. !isApp &&
  17. !!accessStore.accessCode
  18. ) {
  19. // or use access code
  20. authKey = bearer(ACCESS_CODE_PREFIX + accessStore.accessCode);
  21. }
  22. return authKey;
  23. }