authority.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import { reloadAuthorized } from './Authorized';
  2. // use localStorage to store the authority info, which might be sent from server in actual project.
  3. export function getAuthority(str?: string): string | string[] {
  4. const authorityString =
  5. typeof str === 'undefined' && localStorage ? localStorage.getItem('antd-pro-authority') : str;
  6. // authorityString could be admin, "admin", ["admin"]
  7. let authority;
  8. try {
  9. if (authorityString) {
  10. authority = JSON.parse(authorityString);
  11. }
  12. } catch (e) {
  13. authority = authorityString;
  14. }
  15. if (typeof authority === 'string') {
  16. return [authority];
  17. }
  18. // preview.pro.ant.design only do not use in your production.
  19. // preview.pro.ant.design Dedicated environment variable, please do not use it in your project.
  20. if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
  21. return ['admin'];
  22. }
  23. return authority;
  24. }
  25. export function setAuthority(authority: string | string[]): void {
  26. const proAuthority = typeof authority === 'string' ? [authority] : authority;
  27. localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
  28. // auto reload
  29. reloadAuthorized();
  30. }