2018-10-01 14:01:20 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2019-01-23 12:48:22 -08:00
|
|
|
import { Route, Redirect, RouteProps } from 'react-router';
|
2019-01-23 07:15:59 -08:00
|
|
|
import { message } from 'antd';
|
2018-10-01 14:01:20 -07:00
|
|
|
import { AppState } from 'store/reducers';
|
2019-01-17 22:40:30 -08:00
|
|
|
import { authActions } from 'modules/auth';
|
2019-01-23 07:15:59 -08:00
|
|
|
import Loader from 'components/Loader';
|
2019-01-17 22:40:30 -08:00
|
|
|
|
|
|
|
interface OwnProps {
|
|
|
|
onlyLoggedOut?: boolean;
|
|
|
|
}
|
2018-10-01 14:01:20 -07:00
|
|
|
|
|
|
|
interface StateProps {
|
|
|
|
user: AppState['auth']['user'];
|
2019-01-15 12:19:59 -08:00
|
|
|
isCheckingUser: AppState['auth']['isCheckingUser'];
|
2019-01-17 22:40:30 -08:00
|
|
|
hasCheckedUser: AppState['auth']['hasCheckedUser'];
|
|
|
|
authForwardLocation: AppState['auth']['authForwardLocation'];
|
2018-10-01 14:01:20 -07:00
|
|
|
}
|
|
|
|
|
2019-01-17 22:40:30 -08:00
|
|
|
interface DispatchProps {
|
|
|
|
setAuthForwardLocation: typeof authActions['setAuthForwardLocation'];
|
2018-10-01 14:01:20 -07:00
|
|
|
}
|
|
|
|
|
2019-01-17 22:40:30 -08:00
|
|
|
type Props = RouteProps & StateProps & OwnProps & DispatchProps;
|
2018-10-01 14:01:20 -07:00
|
|
|
|
|
|
|
class AuthRoute extends React.Component<Props> {
|
2019-01-17 22:40:30 -08:00
|
|
|
componentDidMount() {
|
|
|
|
this.setAuthForward();
|
|
|
|
}
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
|
|
const { hasCheckedUser } = this.props;
|
|
|
|
// in case we mounted before having checked user
|
|
|
|
if (!prevProps.hasCheckedUser && hasCheckedUser) {
|
|
|
|
this.setAuthForward();
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 14:01:20 -07:00
|
|
|
public render() {
|
2019-01-17 22:40:30 -08:00
|
|
|
const {
|
|
|
|
user,
|
|
|
|
onlyLoggedOut,
|
|
|
|
isCheckingUser,
|
|
|
|
location,
|
|
|
|
authForwardLocation,
|
|
|
|
...routeProps
|
|
|
|
} = this.props;
|
2019-01-15 12:19:59 -08:00
|
|
|
if (isCheckingUser) {
|
2019-01-23 08:35:03 -08:00
|
|
|
return <Loader size="large" tip="Checking authentication status" />;
|
2019-01-15 12:19:59 -08:00
|
|
|
}
|
2018-12-14 11:36:22 -08:00
|
|
|
if ((user && !onlyLoggedOut) || (!user && onlyLoggedOut)) {
|
2018-10-01 14:01:20 -07:00
|
|
|
return <Route {...routeProps} />;
|
|
|
|
} else {
|
2019-01-17 22:40:30 -08:00
|
|
|
let newLocation = { ...location, pathname: '/auth' };
|
|
|
|
if (onlyLoggedOut) {
|
|
|
|
newLocation = authForwardLocation || { ...location, pathname: '/profile' };
|
|
|
|
}
|
2019-03-12 20:08:50 -07:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<noscript className="noScript is-block">
|
|
|
|
This page requires you to login before you can access it, but you have
|
|
|
|
Javascript disabled. Please enable Javascript and login to continue.
|
|
|
|
</noscript>
|
|
|
|
<Redirect to={{ ...newLocation }} />
|
|
|
|
</>
|
|
|
|
);
|
2018-10-01 14:01:20 -07:00
|
|
|
}
|
|
|
|
}
|
2019-01-17 22:40:30 -08:00
|
|
|
private setAuthForward = () => {
|
|
|
|
const {
|
|
|
|
setAuthForwardLocation,
|
|
|
|
location,
|
|
|
|
hasCheckedUser,
|
|
|
|
user,
|
|
|
|
onlyLoggedOut,
|
|
|
|
} = this.props;
|
|
|
|
if (hasCheckedUser && !user && !onlyLoggedOut) {
|
|
|
|
message.warn('Authorization required.');
|
|
|
|
setAuthForwardLocation(location);
|
|
|
|
}
|
|
|
|
};
|
2018-10-01 14:01:20 -07:00
|
|
|
}
|
|
|
|
|
2019-01-17 22:40:30 -08:00
|
|
|
export default connect<StateProps, DispatchProps, OwnProps, AppState>(
|
|
|
|
(state: AppState) => ({
|
|
|
|
user: state.auth.user,
|
|
|
|
isCheckingUser: state.auth.isCheckingUser,
|
|
|
|
hasCheckedUser: state.auth.hasCheckedUser,
|
|
|
|
authForwardLocation: state.auth.authForwardLocation,
|
|
|
|
}),
|
|
|
|
{ setAuthForwardLocation: authActions.setAuthForwardLocation },
|
|
|
|
)(AuthRoute);
|