authority.js 908 B

123456789101112131415161718192021222324252627
  1. // use localStorage to store the authority info, which might be sent from server in actual project.
  2. const { NODE_ENV } = process.env;
  3. export function getAuthority(str) {
  4. // return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
  5. const authorityString =
  6. typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str;
  7. // authorityString could be admin, "admin", ["admin"]
  8. let authority;
  9. try {
  10. authority = JSON.parse(authorityString);
  11. } catch (e) {
  12. authority = authorityString;
  13. }
  14. if (typeof authority === 'string') {
  15. return [authority];
  16. }
  17. if (!authority && NODE_ENV !== 'production') {
  18. return ['admin'];
  19. }
  20. return authority;
  21. }
  22. export function setAuthority(authority) {
  23. const proAuthority = typeof authority === 'string' ? [authority] : authority;
  24. return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
  25. }