github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/views/login/requireLogin.tsx (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  import React from "react";
    12  import { RouteComponentProps, withRouter } from "react-router-dom";
    13  import { connect } from "react-redux";
    14  
    15  import { AdminUIState } from "src/redux/state";
    16  import { selectLoginState, LoginState, getLoginPage } from "src/redux/login";
    17  
    18  interface RequireLoginProps {
    19    loginState: LoginState;
    20  }
    21  
    22  class RequireLogin extends React.Component<RouteComponentProps & RequireLoginProps> {
    23    componentDidMount() {
    24      this.checkLogin();
    25    }
    26  
    27    componentDidUpdate() {
    28      this.checkLogin();
    29    }
    30  
    31    checkLogin() {
    32      const { location, history } = this.props;
    33  
    34      if (!this.hideLoginPage()) {
    35        history.push(getLoginPage(location));
    36      }
    37    }
    38  
    39    hideLoginPage() {
    40      return this.props.loginState.hideLoginPage();
    41    }
    42  
    43    render() {
    44      if (!this.hideLoginPage()) {
    45        return null;
    46      }
    47  
    48      return this.props.children;
    49    }
    50  }
    51  
    52  // tslint:disable-next-line:variable-name
    53  const RequireLoginConnected = withRouter(connect(
    54    (state: AdminUIState) => {
    55      return {
    56        loginState: selectLoginState(state),
    57      };
    58    },
    59  )(RequireLogin));
    60  
    61  export default RequireLoginConnected;