Stack Exchange
Authorization permissions
I use ra-loopback. And I want to add a role during authorization. I tried to add a role like this:
./authClient.js
import storage from './storage';
import {decode} from 'jsonwebtoken';

export const authClient = (loginApiUrl, noAccessPage = '/login') => {

    return (type, params) => {
        if (type === 'AUTH_LOGIN') {
            const request = new Request(loginApiUrl, {
                method: 'POST',
                body: JSON.stringify(params),
                headers: new Headers({ 'Content-Type': 'application/json' }),
            });
            return fetch(request)
                .then(response => {
                    if (response.status < 200 || response.status >= 300) {
                        throw new Error(response.statusText);
                    }
                    return response.json();
                })
                .then(({token}) => {
                    const decoded = decode(token);
                    storage.save('lbtoken',token);
                    storage.save('role', decoded.role);
                });
        }
        if (type === 'AUTH_LOGOUT') {
            storage.remove('lbtoken');
            return Promise.resolve();
        }
        if (type === 'AUTH_ERROR') {
            const status  = params.message.status;
            if (status === 401 || status === 403) {
                storage.remove('lbtoken');
                return Promise.reject();
            }
            return Promise.resolve();
        }
        if (type === 'AUTH_CHECK') {
            const token = storage.load('lbtoken');
            if (token && token.id) {
                return Promise.resolve();
            } else {
                storage.remove('lbtoken');
                return Promise.reject({ redirectTo: noAccessPage });
            }
        }
        if (type === 'AUTH_GET_PERMISSIONS') {
            const role = localStorage.getItem('role');
            return role ? Promise.resolve(role) : Promise.reject();
        }
        return Promise.reject('Unknown method');
    };
};

When i tried to login, i get an error 'Cannot read property 'role' of null'.
Actually, i want to add permissions to Admin. 
I want some resources to be visible only to admin. I know how it does, but I don`t know how to add role to user during authorization.

try this:
import storage from './storage';
import {
  decode
} from 'jsonwebtoken';

export const authClient = (loginApiUrl, noAccessPage = '/login') => {

  return (type, params) => {
    if (type === 'AUTH_LOGIN') {
      const request = new Request(loginApiUrl, {
        method: 'POST',
        body: JSON.stringify(params),
        headers: new Headers({
          'Content-Type': 'application/json'
        }),
      });
      return fetch(request)
        .then(response => {
          if (response.status < 200 || response.status >= 300) {
            throw new Error(response.statusText);
          }
          return response.json();
        })
        .then(({
          token
        }) => {
          const decoded = decode(token);
          storage.save('lbtoken', token);
          storage.save('role', decoded.role);
        });
    }
    if (type === 'AUTH_LOGOUT') {
      storage.remove('lbtoken');
      storage.remove('role');
      return Promise.resolve();
    }
    if (type === 'AUTH_ERROR') {
      const status = params.message.status;
      if (status === 401 || status === 403) {
        storage.remove('lbtoken');
        storage.remove('role');
        return Promise.reject();
      }
      return Promise.resolve();
    }
    if (type === 'AUTH_CHECK') {
      const token = storage.load('lbtoken');
      if (token && token.id) {
        return Promise.resolve();
      } else {
        storage.remove('lbtoken');
        storage.remove('role');
        return Promise.reject({
          redirectTo: noAccessPage
        });
      }
    }
    if (type === 'AUTH_GET_PERMISSIONS') {
      const role = localStorage.getItem('role');

      if (role) {
        return Promise.resolve(role);
      } else {
        storage.remove('lbtoken');
        storage.remove('role');
        return Promise.reject({
          redirectTo: noAccessPage
        });
      }
    }
    return Promise.reject('Unknown method');
  };
}; 

