Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Fix login page (#28)
Browse files Browse the repository at this point in the history
* added fix

* added loading state to all pages
  • Loading branch information
wtfthefirefox authored Aug 17, 2023
1 parent 1204b51 commit dd7169e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
21 changes: 18 additions & 3 deletions ui-dashboard/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const App: React.FC = () => {
const [user, setUser] = React.useState<User>();
const [isSupportFormOpen, setIsSupportFormOpen] = React.useState<boolean>(false);
const [isFormSubmitting, setIsFormSubmitting] = React.useState<boolean>(false);
const [isLoading, setIsLoading] = React.useState<boolean>(true);

const loadUserInfo = async () => {
let newMerchantId = merchantId;
Expand All @@ -112,7 +113,11 @@ const App: React.FC = () => {
posthog?.reset(true);
}

navigate("/login");
navigate("/login", {
state: {
isNeedLogout: true
}
});
}
}
};
Expand All @@ -127,7 +132,11 @@ const App: React.FC = () => {
posthog?.reset(true);
}

navigate("/login");
navigate("/login", {
state: {
isNeedLogout: true
}
});
}
}
};
Expand All @@ -154,6 +163,7 @@ const App: React.FC = () => {
if (!newMerchantId) return;

await getMerchant(newMerchantId);
setIsLoading(false);
};

await getCookie();
Expand Down Expand Up @@ -210,7 +220,11 @@ const App: React.FC = () => {
}

await authProvider.logout();
navigate("/login");
navigate("/login", {
state: {
isNeedLogout: true
}
});
};

const userMenu: MenuProps["items"] = [
Expand Down Expand Up @@ -277,6 +291,7 @@ const App: React.FC = () => {
)}
</RouteContext.Consumer>
}
loading={isLoading}
actionsRender={() => {
return [
!isManageMerchantsActive ? (
Expand Down
24 changes: 22 additions & 2 deletions ui-dashboard/src/pages/login-page/login-page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import "./login-page.scss";

import * as React from "react";
import {useNavigate} from "react-router-dom";
import {AxiosError} from "axios";
import {useNavigate, useLocation} from "react-router-dom";
import {Modal, Button, Typography, Form, Input, notification} from "antd";
import {GoogleOutlined, CheckOutlined} from "@ant-design/icons";
import logoImg from "/fav/android-chrome-192x192.png";
Expand All @@ -15,12 +16,17 @@ import SpinWithMask from "src/components/spin-with-mask/spin-with-mask";

const b = bevis("login-page");

interface LoginState {
isNeedLogout: boolean;
}

const LoginPage: React.FC = () => {
const [form] = Form.useForm<UserCreateForm>();
const [api, contextHolder] = notification.useNotification();
const [isFormSubmitting, setIsFormSubmitting] = React.useState<boolean>(false);
const [providersList, setProvidersList] = React.useState<AuthProvider[]>([]);
const navigate = useNavigate();
const state: LoginState = useLocation().state;

const openNotification = (title: string, description: string) => {
api.info({
Expand Down Expand Up @@ -60,7 +66,21 @@ const LoginPage: React.FC = () => {

useMount(async () => {
window.addEventListener("popstate", () => navigate("/login", {replace: true}));
localStorage.remove("merchantId");

if (state?.isNeedLogout) {
localStorage.remove("merchantId");
} else {
try {
await authProvider.getCookie();
await authProvider.getMe();
navigate("/");
} catch (e) {
if (e instanceof AxiosError && e.response?.status === 401) {
localStorage.remove("merchantId");
}
}
}

const availProviders = await authProvider.getProviders();
setProvidersList(availProviders ?? []);
});
Expand Down

0 comments on commit dd7169e

Please sign in to comment.