Skip to content

Commit 9897991

Browse files
authored
Move Login to Typescript Swagger API (#2839)
Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
1 parent c0cf735 commit 9897991

File tree

9 files changed

+84
-78
lines changed

9 files changed

+84
-78
lines changed

portal-ui/src/MainRouter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import LoadingComponent from "./common/LoadingComponent";
2121
import AppConsole from "./screens/Console/ConsoleKBar";
2222
import { baseUrl } from "./history";
2323

24-
const Login = React.lazy(() => import("./screens/LoginPage/LoginPage"));
24+
const Login = React.lazy(() => import("./screens/LoginPage/Login"));
2525
const Logout = React.lazy(() => import("./screens/LogoutPage/LogoutPage"));
2626
const LoginCallback = React.lazy(
2727
() => import("./screens/LoginPage/LoginCallback")

portal-ui/src/screens/LoginPage/LoginPage.tsx renamed to portal-ui/src/screens/LoginPage/Login.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,16 @@
1717
import React, { Fragment, useEffect } from "react";
1818
import { useNavigate } from "react-router-dom";
1919
import { Box, Button, Loader, LoginWrapper, RefreshIcon } from "mds";
20-
import { loginStrategyType, redirectRule } from "./types";
20+
import { loginStrategyType } from "./login.types";
2121
import MainError from "../Console/Common/MainError/MainError";
2222
import { AppState, useAppDispatch } from "../../store";
2323
import { useSelector } from "react-redux";
2424
import { getFetchConfigurationAsync, getVersionAsync } from "./loginThunks";
2525
import { resetForm } from "./loginSlice";
2626
import StrategyForm from "./StrategyForm";
27-
import { redirectRules } from "../../utils/sortFunctions";
2827
import { getLogoVar } from "../../config";
29-
30-
export interface LoginStrategyPayload {
31-
accessKey: string;
32-
secretKey: string;
33-
sts?: string;
34-
}
28+
import { RedirectRule } from "api/consoleApi";
29+
import { redirectRules } from "./login.utils";
3530

3631
export const getTargetPath = () => {
3732
let targetPath = "/";
@@ -90,7 +85,7 @@ const Login = () => {
9085
switch (loginStrategy.loginStrategy) {
9186
case loginStrategyType.redirect:
9287
case loginStrategyType.form: {
93-
let redirectItems: redirectRule[] = [];
88+
let redirectItems: RedirectRule[] = [];
9489

9590
if (
9691
loginStrategy.redirectRules &&

portal-ui/src/screens/LoginPage/StrategyForm.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ import { LinearProgress } from "@mui/material";
3838
import { AppState, useAppDispatch } from "../../store";
3939
import { useSelector } from "react-redux";
4040
import { doLoginAsync } from "./loginThunks";
41-
import { IStrategyForm } from "./types";
41+
import { RedirectRule } from "api/consoleApi";
4242

43-
const StrategyForm = ({ redirectRules }: IStrategyForm) => {
43+
const StrategyForm = ({ redirectRules }: { redirectRules: RedirectRule[] }) => {
4444
const dispatch = useAppDispatch();
4545

4646
const [ssoOptionsOpen, ssoOptionsSetOpen] = useState<boolean>(false);
@@ -137,7 +137,7 @@ const StrategyForm = ({ redirectRules }: IStrategyForm) => {
137137
setAnchorEl(e.currentTarget);
138138
return;
139139
}
140-
submitSSOInitRequest(redirectRules[0].redirect);
140+
submitSSOInitRequest(`${redirectRules[0].redirect}`);
141141
}}
142142
/>
143143
{redirectRules.length > 1 && (

portal-ui/src/screens/LoginPage/types.ts renamed to portal-ui/src/screens/LoginPage/login.types.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
export interface ILoginDetails {
18-
loginStrategy: loginStrategyType;
19-
redirectRules: redirectRule[];
20-
isK8S?: boolean;
21-
animatedLogin?: boolean;
22-
}
23-
24-
export interface redirectRule {
25-
redirect: string;
26-
displayName: string;
27-
serviceType?: string;
28-
}
29-
30-
export interface IStrategyForm {
31-
redirectRules: redirectRule[];
32-
}
33-
3417
export enum loginStrategyType {
3518
unknown = "unknown",
3619
form = "form",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 { RedirectRule } from "api/consoleApi";
18+
19+
export const redirectRules = (a: RedirectRule, b: RedirectRule) => {
20+
if (a.displayName && b.displayName) {
21+
if (a.displayName > b.displayName) {
22+
return 1;
23+
}
24+
if (a.displayName < b.displayName) {
25+
return -1;
26+
}
27+
}
28+
return 0;
29+
};

portal-ui/src/screens/LoginPage/loginSlice.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
18-
import { ILoginDetails, loginStrategyType } from "./types";
18+
import { LoginDetails } from "api/consoleApi";
1919
import {
2020
doLoginAsync,
2121
getFetchConfigurationAsync,
@@ -28,7 +28,7 @@ export interface LoginState {
2828
sts: string;
2929
useSTS: boolean;
3030
backgroundAnimation: boolean;
31-
loginStrategy: ILoginDetails;
31+
loginStrategy: LoginDetails;
3232
loginSending: boolean;
3333
loadingFetchConfiguration: boolean;
3434
latestMinIOVersion: string;
@@ -44,7 +44,7 @@ const initialState: LoginState = {
4444
sts: "",
4545
useSTS: false,
4646
loginStrategy: {
47-
loginStrategy: loginStrategyType.unknown,
47+
loginStrategy: undefined,
4848
redirectRules: [],
4949
},
5050
loginSending: false,

portal-ui/src/screens/LoginPage/loginThunks.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
import { createAsyncThunk } from "@reduxjs/toolkit";
1818
import { AppState } from "../../store";
19-
import api from "../../common/api";
20-
import { ErrorResponseHandler } from "../../common/types";
2119
import { setErrorSnackMessage, userLogged } from "../../systemSlice";
22-
import { ILoginDetails } from "./types";
2320
import { setNavigateTo } from "./loginSlice";
24-
import { getTargetPath, LoginStrategyPayload } from "./LoginPage";
21+
import { getTargetPath } from "./Login";
22+
import { api } from "api";
23+
import {
24+
CheckVersionResponse,
25+
Error,
26+
HttpResponse,
27+
LoginDetails,
28+
LoginRequest,
29+
} from "api/consoleApi";
30+
import { errorToHandler } from "api/errors";
2531

2632
export const doLoginAsync = createAsyncThunk(
2733
"login/doLoginAsync",
@@ -32,63 +38,65 @@ export const doLoginAsync = createAsyncThunk(
3238
const sts = state.login.sts;
3339
const useSTS = state.login.useSTS;
3440

35-
let loginStrategyPayload: LoginStrategyPayload = {
41+
let payload: LoginRequest = {
3642
accessKey,
3743
secretKey,
3844
};
3945
if (useSTS) {
40-
loginStrategyPayload = {
46+
payload = {
4147
accessKey,
4248
secretKey,
4349
sts,
4450
};
4551
}
4652

47-
return api
48-
.invoke("POST", "/api/v1/login", loginStrategyPayload)
49-
.then((res) => {
53+
return api.login
54+
.login(payload)
55+
.then((res: HttpResponse<void, Error>) => {
5056
// We set the state in redux
5157
dispatch(userLogged(true));
5258
localStorage.setItem("userLoggedIn", accessKey);
5359
dispatch(setNavigateTo(getTargetPath()));
5460
})
55-
.catch((err) => {
56-
dispatch(setErrorSnackMessage(err));
61+
.catch(async (res: HttpResponse<void, Error>) => {
62+
const err = (await res.json()) as Error;
63+
dispatch(setErrorSnackMessage(errorToHandler(err)));
64+
return rejectWithValue(false);
5765
});
5866
}
5967
);
6068
export const getFetchConfigurationAsync = createAsyncThunk(
6169
"login/getFetchConfigurationAsync",
62-
async (_, { getState, rejectWithValue, dispatch }) => {
63-
return api
64-
.invoke("GET", "/api/v1/login")
65-
.then((loginDetails: ILoginDetails) => {
66-
return loginDetails;
70+
async (_, { dispatch, rejectWithValue }) => {
71+
return api.login
72+
.loginDetail()
73+
.then((res: HttpResponse<LoginDetails, Error>) => {
74+
if (res.data) {
75+
return res.data;
76+
}
6777
})
68-
.catch((err: ErrorResponseHandler) => {
69-
dispatch(setErrorSnackMessage(err));
78+
.catch(async (res: HttpResponse<LoginDetails, Error>) => {
79+
const err = (await res.json()) as Error;
80+
dispatch(setErrorSnackMessage(errorToHandler(err)));
81+
return rejectWithValue(false);
7082
});
7183
}
7284
);
7385

7486
export const getVersionAsync = createAsyncThunk(
7587
"login/getVersionAsync",
7688
async (_, { getState, rejectWithValue, dispatch }) => {
77-
return api
78-
.invoke("GET", "/api/v1/check-version")
79-
.then(
80-
({
81-
current_version,
82-
latest_version,
83-
}: {
84-
current_version: string;
85-
latest_version: string;
86-
}) => {
87-
return latest_version;
89+
return api.checkVersion
90+
.checkMinIoVersion()
91+
.then((res: HttpResponse<CheckVersionResponse, Error>) => {
92+
if (res.data !== undefined) {
93+
return res.data.latest_version;
8894
}
89-
)
90-
.catch((err: ErrorResponseHandler) => {
91-
return err.errorMessage;
95+
})
96+
.catch(async (res: HttpResponse<CheckVersionResponse, Error>) => {
97+
const err = (await res.json()) as Error;
98+
dispatch(setErrorSnackMessage(errorToHandler(err)));
99+
return rejectWithValue(false);
92100
});
93101
}
94102
);

portal-ui/src/utils/sortFunctions.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
import { Policy } from "../api/consoleApi";
18-
import { redirectRule } from "../screens/LoginPage/types";
17+
import { Policy } from "api/consoleApi";
1918

2019
interface userInterface {
2120
accessKey: string;
@@ -71,13 +70,3 @@ export const policyDetailsSort = (
7170
// a must be equal to b
7271
return 0;
7372
};
74-
75-
export const redirectRules = (a: redirectRule, b: redirectRule) => {
76-
if (a.displayName > b.displayName) {
77-
return 1;
78-
}
79-
if (a.displayName < b.displayName) {
80-
return -1;
81-
}
82-
return 0;
83-
};

portal-ui/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"noEmit": true,
1616
"jsx": "react-jsx",
1717
"downlevelIteration": true,
18-
"noFallthroughCasesInSwitch": true
18+
"noFallthroughCasesInSwitch": true,
19+
"baseUrl": "./src",
20+
"rootDir": "./src"
1921
},
2022
"include": ["src"]
2123
}

0 commit comments

Comments
 (0)