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

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2023 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 { setErrorSnackMessage } from "../../systemSlice";
    19  import { api } from "api";
    20  import { errorToHandler } from "api/errors";
    21  import {
    22    saveSessionResponse,
    23    setSessionLoadingState,
    24  } from "../Console/consoleSlice";
    25  import { SessionCallStates } from "../Console/consoleSlice.types";
    26  
    27  import {
    28    globalSetDistributedSetup,
    29    setAnonymousMode,
    30    setOverrideStyles,
    31    userLogged,
    32  } from "../../../src/systemSlice";
    33  import { getOverrideColorVariants } from "../../utils/stylesUtils";
    34  import { AppState } from "../../store";
    35  
    36  export const fetchSession = createAsyncThunk(
    37    "session/fetchSession",
    38    async (_, { getState, dispatch, rejectWithValue }) => {
    39      const state = getState() as AppState;
    40      const pathnameParts = state.system.locationPath.split("/");
    41      const screen = pathnameParts.length > 2 ? pathnameParts[1] : "";
    42  
    43      return api.session
    44        .sessionCheck()
    45        .then((res) => {
    46          dispatch(userLogged(true));
    47          dispatch(saveSessionResponse(res.data));
    48          dispatch(globalSetDistributedSetup(res.data.distributedMode || false));
    49  
    50          if (res.data.customStyles && res.data.customStyles !== "") {
    51            const overrideColorVariants = getOverrideColorVariants(
    52              res.data.customStyles,
    53            );
    54  
    55            if (overrideColorVariants !== false) {
    56              dispatch(setOverrideStyles(overrideColorVariants));
    57            }
    58          }
    59        })
    60        .catch(async (res) => {
    61          if (screen === "browser") {
    62            const bucket = pathnameParts.length >= 3 ? pathnameParts[2] : "";
    63            // no bucket, no business
    64            if (bucket === "") {
    65              return;
    66            }
    67            // before marking the session as done, let's check if the bucket is publicly accessible (anonymous)
    68            api.buckets
    69              .listObjects(
    70                bucket,
    71                { limit: 1 },
    72                { headers: { "X-Anonymous": "1" } },
    73              )
    74              .then(() => {
    75                dispatch(setAnonymousMode());
    76              })
    77              .catch((res) => {
    78                dispatch(setErrorSnackMessage(errorToHandler(res.error)));
    79              })
    80              .finally(() => {
    81                // TODO: we probably need a thunk for this api since setting the state here is hacky,
    82                // we can use a state to let the ProtectedRoutes know when to render the elements
    83                dispatch(setSessionLoadingState(SessionCallStates.Done));
    84              });
    85          } else {
    86            dispatch(setSessionLoadingState(SessionCallStates.Done));
    87            dispatch(setErrorSnackMessage(errorToHandler(res.error)));
    88          }
    89          return rejectWithValue(res.error);
    90        });
    91    },
    92  );