authority.js 908 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 3
const { NODE_ENV } = process.env;

4
export function getAuthority(str) {
5
  // return localStorage.getItem('antd-pro-authority') || ['admin', 'user'];
6 7 8
  const authorityString =
    typeof str === 'undefined' ? localStorage.getItem('antd-pro-authority') : str;
  // authorityString could be admin, "admin", ["admin"]
afc163's avatar
afc163 committed
9
  let authority;
10
  try {
afc163's avatar
afc163 committed
11
    authority = JSON.parse(authorityString);
12 13 14 15 16
  } catch (e) {
    authority = authorityString;
  }
  if (typeof authority === 'string') {
    return [authority];
17
  }
18 19 20
  if (!authority && NODE_ENV !== 'production') {
    return ['admin'];
  }
21
  return authority;
ddcat1115's avatar
ddcat1115 committed
22 23 24
}

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