import { PrimaryButton } from 'office-ui-fabric-react/lib-commonjs/Button'; import * as React from 'react'; import { UserInfo } from '../userProfile'; import { History, WithRouter } from '../withRouter'; import classNames from './withAuth.classNames'; export interface WithAuthActions { login(history: History): void; } export interface WithAuthAttributes { user: UserInfo; isLogin?(history: History): boolean; } export type WithAuthProps = WithAuthAttributes & WithAuthActions; export const withAuth =

>(Inner: React.ComponentClass

, ...roles: string[]) => class WithAuth extends React.Component

{ public render(): JSX.Element { if (this.props.user || (this.props.isLogin && this.props.isLogin(this.props.history))) { if (this.props.user && !this.authorized) { return (

You don't have access

); } else { return ; } } else { return (

Please sign in

); } } public get login(): () => void { return () => { this.props.login(this.props.history); }; } private get authorized(): boolean { const userRoles = (this.props.user ? this.props.user.roles : []) || []; return this.props.user && (roles.length === 0 || !!roles.find(x => userRoles.indexOf(x) >= 0)); } };