@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
83 lines (65 loc) • 3.15 kB
JavaScript
var _class, _temp;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import PropTypes from 'prop-types';
import { Route, Redirect } from 'react-router-dom';
import Configurable from './Configurable';
import AuthUtils from '../util/AuthUtils';
// LJV TODO Create a no-permissions page to use for unauthorized users
var AuthRoute = (_temp = _class = function (_React$Component) {
_inherits(AuthRoute, _React$Component);
function AuthRoute(props) {
_classCallCheck(this, AuthRoute);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
_this.state = {
user: null
};
return _this;
}
AuthRoute.prototype.getChildContext = function getChildContext() {
return {
user: this.state.user
};
};
AuthRoute.prototype.componentDidMount = function componentDidMount() {
var _this2 = this;
AuthUtils.getLoggedInUserInfo(function (userInfo) {
_this2.setState({
user: userInfo
});
});
};
AuthRoute.prototype.render = function render() {
// if authentication is via XML, handled here in JavaScript, make sure the user is logged in.
if (this.props.authType === 'XML') {
if (AuthUtils.isLoggedIn(this.props.requiredRole)) {
return React.createElement(Route, this.props);
}
// insufficient credentials => redirect to our login page
return React.createElement(Redirect, {
to: {
pathname: AuthUtils.config.ALL.loginPage,
state: {
referrer: this.props.location
}
}
});
}
// If this route doesn't require any special credentials or it does and the currently logged-in user meets them
if (!this.props.requiredRole || AuthUtils.isLoggedIn(this.props.requiredRole)) {
return React.createElement(Route, this.props);
}
// insufficient credentials (provided by external authentication system) => don't let the user through
return null;
};
return AuthRoute;
}(React.Component), _class.defaultProps = {
requiredRole: null,
location: null, // This should be filled in by the router
authType: 'NONE'
}, _class.displayName = 'AuthRoute', _class.childContextTypes = {
user: PropTypes.any
}, _temp);
export default Configurable(AuthRoute);