2018-10-01 14:01:20 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Route, Redirect, RouteProps } from 'react-router-dom';
|
2019-01-15 12:19:59 -08:00
|
|
|
import { Spin } from 'antd';
|
2018-10-01 14:01:20 -07:00
|
|
|
import { AppState } from 'store/reducers';
|
|
|
|
|
|
|
|
interface StateProps {
|
|
|
|
user: AppState['auth']['user'];
|
2019-01-15 12:19:59 -08:00
|
|
|
isCheckingUser: AppState['auth']['isCheckingUser'];
|
2018-10-01 14:01:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
interface OwnProps {
|
|
|
|
onlyLoggedOut?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Props = RouteProps & StateProps & OwnProps;
|
|
|
|
|
|
|
|
class AuthRoute extends React.Component<Props> {
|
|
|
|
public render() {
|
2019-01-15 12:19:59 -08:00
|
|
|
const { user, onlyLoggedOut, isCheckingUser, location, ...routeProps } = this.props;
|
|
|
|
if (isCheckingUser) {
|
|
|
|
return <Spin />;
|
|
|
|
}
|
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 {
|
|
|
|
// TODO: redirect to desired destination after auth
|
|
|
|
// TODO: Show alert that claims they need to be logged in
|
2019-01-15 12:19:59 -08:00
|
|
|
const pathname = onlyLoggedOut ? '/profile' : '/auth';
|
|
|
|
return <Redirect to={{ ...location, pathname }} />;
|
2018-10-01 14:01:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect((state: AppState) => ({
|
|
|
|
user: state.auth.user,
|
2019-01-15 12:19:59 -08:00
|
|
|
isCheckingUser: state.auth.isCheckingUser,
|
2018-10-01 14:01:20 -07:00
|
|
|
}))(AuthRoute);
|