github.com/minio/console@v1.4.1/web-app/src/screens/LoginPage/Login.tsx (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import React, { Fragment, useEffect } from "react";
    18  import { useNavigate } from "react-router-dom";
    19  import { Box, Button, Loader, LoginWrapper, RefreshIcon } from "mds";
    20  import { loginStrategyType } from "./login.types";
    21  import MainError from "../Console/Common/MainError/MainError";
    22  import { AppState, useAppDispatch } from "../../store";
    23  import { useSelector } from "react-redux";
    24  import { getFetchConfigurationAsync } from "./loginThunks";
    25  import { resetForm } from "./loginSlice";
    26  import StrategyForm from "./StrategyForm";
    27  import { getLogoVar } from "../../config";
    28  import { RedirectRule } from "api/consoleApi";
    29  import { redirectRules } from "./login.utils";
    30  import { setHelpName } from "../../systemSlice";
    31  
    32  export const getTargetPath = () => {
    33    let targetPath = "/browser";
    34    if (
    35      localStorage.getItem("redirect-path") &&
    36      localStorage.getItem("redirect-path") !== ""
    37    ) {
    38      targetPath = `${localStorage.getItem("redirect-path")}`;
    39      localStorage.setItem("redirect-path", "");
    40    }
    41    return targetPath;
    42  };
    43  
    44  const Login = () => {
    45    const dispatch = useAppDispatch();
    46    const navigate = useNavigate();
    47  
    48    const loginStrategy = useSelector(
    49      (state: AppState) => state.login.loginStrategy,
    50    );
    51    const loadingFetchConfiguration = useSelector(
    52      (state: AppState) => state.login.loadingFetchConfiguration,
    53    );
    54    const navigateTo = useSelector((state: AppState) => state.login.navigateTo);
    55  
    56    const isK8S = useSelector((state: AppState) => state.login.isK8S);
    57  
    58    const backgroundAnimation = useSelector(
    59      (state: AppState) => state.login.backgroundAnimation,
    60    );
    61  
    62    useEffect(() => {
    63      if (navigateTo !== "") {
    64        dispatch(resetForm());
    65        navigate(navigateTo);
    66      }
    67    }, [navigateTo, dispatch, navigate]);
    68  
    69    useEffect(() => {
    70      if (loadingFetchConfiguration) {
    71        dispatch(getFetchConfigurationAsync());
    72      }
    73    }, [loadingFetchConfiguration, dispatch]);
    74  
    75    let loginComponent;
    76  
    77    switch (loginStrategy.loginStrategy) {
    78      case loginStrategyType.redirect:
    79      case loginStrategyType.form: {
    80        let redirectItems: RedirectRule[] = [];
    81  
    82        if (
    83          loginStrategy.redirectRules &&
    84          loginStrategy.redirectRules.length > 0
    85        ) {
    86          redirectItems = [...loginStrategy.redirectRules].sort(redirectRules);
    87        }
    88  
    89        loginComponent = <StrategyForm redirectRules={redirectItems} />;
    90        break;
    91      }
    92      default:
    93        loginComponent = (
    94          <Box
    95            sx={{
    96              textAlign: "center",
    97              "& .loadingLoginStrategy": {
    98                textAlign: "center",
    99                width: 40,
   100                height: 40,
   101              },
   102              "& .buttonRetry": {
   103                display: "flex",
   104                justifyContent: "center",
   105              },
   106            }}
   107          >
   108            {loadingFetchConfiguration ? (
   109              <Loader className={"loadingLoginStrategy"} />
   110            ) : (
   111              <Fragment>
   112                <Box>
   113                  <p style={{ textAlign: "center" }}>
   114                    An error has occurred
   115                    <br />
   116                    The backend cannot be reached.
   117                  </p>
   118                </Box>
   119                <div className={"buttonRetry"}>
   120                  <Button
   121                    onClick={() => {
   122                      dispatch(getFetchConfigurationAsync());
   123                    }}
   124                    icon={<RefreshIcon />}
   125                    iconLocation={"end"}
   126                    variant="regular"
   127                    id="retry"
   128                    label={"Retry"}
   129                  />
   130                </div>
   131              </Fragment>
   132            )}
   133          </Box>
   134        );
   135    }
   136  
   137    let docsURL = "https://min.io/docs/minio/linux/index.html?ref=con";
   138    if (isK8S) {
   139      docsURL =
   140        "https://min.io/docs/minio/kubernetes/upstream/index.html?ref=con";
   141    }
   142  
   143    useEffect(() => {
   144      dispatch(setHelpName("login"));
   145      // eslint-disable-next-line react-hooks/exhaustive-deps
   146    }, []);
   147  
   148    return (
   149      <Fragment>
   150        <MainError />
   151        <LoginWrapper
   152          logoProps={{ applicationName: "console", subVariant: getLogoVar() }}
   153          form={loginComponent}
   154          formFooter={
   155            <Box
   156              sx={{
   157                "& .separator": {
   158                  marginLeft: 4,
   159                  marginRight: 4,
   160                },
   161              }}
   162            >
   163              <a href={docsURL} target="_blank" rel="noopener">
   164                Documentation
   165              </a>
   166              <span className={"separator"}>|</span>
   167              <a
   168                href="https://github.com/minio/minio"
   169                target="_blank"
   170                rel="noopener"
   171              >
   172                GitHub
   173              </a>
   174              <span className={"separator"}>|</span>
   175              <a
   176                href="https://subnet.min.io/?ref=con"
   177                target="_blank"
   178                rel="noopener"
   179              >
   180                Support
   181              </a>
   182              <span className={"separator"}>|</span>
   183              <a
   184                href="https://min.io/download/?ref=con"
   185                target="_blank"
   186                rel="noopener"
   187              >
   188                Download
   189              </a>
   190            </Box>
   191          }
   192          promoHeader={
   193            <span style={{ fontSize: 28 }}>High-Performance Object Store</span>
   194          }
   195          promoInfo={
   196            <span style={{ fontSize: 14, lineHeight: 1 }}>
   197              MinIO is a cloud-native object store built to run on any
   198              infrastructure - public, private or edge clouds. Primary use cases
   199              include data lakes, databases, AI/ML, SaaS applications and fast
   200              backup & recovery. MinIO is dual licensed under GNU AGPL v3 and
   201              commercial license. To learn more, visit{" "}
   202              <a href={"https://min.io/?ref=con"} target="_blank" rel="noopener">
   203                www.min.io
   204              </a>
   205              .
   206            </span>
   207          }
   208          backgroundAnimation={backgroundAnimation}
   209        />
   210      </Fragment>
   211    );
   212  };
   213  
   214  export default Login;