'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = getCurrentuserRole;

var _cookies = require('cookies');

var _cookies2 = _interopRequireDefault(_cookies);

var _jwtSimple = require('jwt-simple');

var _jwtSimple2 = _interopRequireDefault(_jwtSimple);

var _ = require('../../../');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Handlebars helper, to print className and escape it string
 */
function getCurrentuserRole(obj) {
  if (typeof obj.express !== 'undefined' && obj.express !== null) {
    var cookies = new _cookies2.default(obj.express.req, obj.express.res, {
      secure: _.config.cookie.secure
    });
    var token = cookies.get('x-access-token');
    if (typeof token !== 'undefined' && token !== null && token !== '') {
      var secret = _.config.users.secret;
      var decoded = _jwtSimple2.default.decode(token, secret);
      var user = _.User.utils.findSync(decoded.iss);
      if (user && user.role) return user.role.workflow;

      return null;
    }
  }
  return '';
}