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

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 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 { createAsyncThunk } from "@reduxjs/toolkit";
    18  import { AppState } from "../../store";
    19  import {
    20    setDarkMode,
    21    setErrorSnackMessage,
    22    userLogged,
    23  } from "../../systemSlice";
    24  import { setNavigateTo } from "./loginSlice";
    25  import { getTargetPath } from "./Login";
    26  import { api } from "api";
    27  import { ApiError, LoginRequest } from "api/consoleApi";
    28  import { errorToHandler } from "api/errors";
    29  import { isDarkModeOn } from "../../utils/stylesUtils";
    30  
    31  export const doLoginAsync = createAsyncThunk(
    32    "login/doLoginAsync",
    33    async (_, { getState, rejectWithValue, dispatch }) => {
    34      const state = getState() as AppState;
    35      const accessKey = state.login.accessKey;
    36      const secretKey = state.login.secretKey;
    37      const sts = state.login.sts;
    38      const useSTS = state.login.useSTS;
    39  
    40      let payload: LoginRequest = {
    41        accessKey,
    42        secretKey,
    43      };
    44      if (useSTS) {
    45        payload = {
    46          accessKey,
    47          secretKey,
    48          sts,
    49        };
    50      }
    51  
    52      return api.login
    53        .login(payload)
    54        .then((res) => {
    55          const darkModeEnabled = isDarkModeOn(); // If null, then we set the dark mode as disabled per requirement. If configuration al ready set, then we establish this configuration
    56  
    57          // We set the state in redux
    58          dispatch(userLogged(true));
    59          localStorage.setItem("userLoggedIn", accessKey);
    60          dispatch(setNavigateTo(getTargetPath()));
    61          dispatch(setDarkMode(!!darkModeEnabled));
    62        })
    63        .catch(async (res) => {
    64          const err = (await res.json()) as ApiError;
    65          dispatch(setErrorSnackMessage(errorToHandler(err)));
    66          return rejectWithValue(false);
    67        });
    68    },
    69  );
    70  export const getFetchConfigurationAsync = createAsyncThunk(
    71    "login/getFetchConfigurationAsync",
    72    async (_, { dispatch, rejectWithValue }) => {
    73      return api.login
    74        .loginDetail()
    75        .then((res) => {
    76          if (res.data) {
    77            return res.data;
    78          }
    79        })
    80        .catch(async (res) => {
    81          const err = (await res.json()) as ApiError;
    82          dispatch(setErrorSnackMessage(errorToHandler(err)));
    83          return rejectWithValue(false);
    84        });
    85    },
    86  );