authority.js 798 Bytes
Newer Older
ddcat1115's avatar
ddcat1115 committed
1
// use localStorage to store the authority info, which might be sent from server in actual project.
2
export function getAuthority(str) {
3
  // return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
4 5 6
  const authorityString =
    typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str;
  // authorityString could be admin, "admin", ["admin"]
afc163's avatar
afc163 committed
7
  let authority;
8
  try {
afc163's avatar
afc163 committed
9
    authority = JSON.parse(authorityString);
10 11 12 13 14
  } catch (e) {
    authority = authorityString;
  }
  if (typeof authority === 'string') {
    return [authority];
15
  }
16
  return authority;
ddcat1115's avatar
ddcat1115 committed
17 18 19
}

export function setAuthority(authority) {
20 21
  const proAuthority = typeof authority === 'string' ? [authority] : authority;
  return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
ddcat1115's avatar
ddcat1115 committed
22
}