Skip to content

Commit

Permalink
Only logout if the server returns 401
Browse files Browse the repository at this point in the history
  • Loading branch information
lil5 committed Feb 21, 2024
1 parent 0c813a0 commit 1f9cd5a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
59 changes: 51 additions & 8 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ToastProvider } from "./providers/ToastProvider";
import { useTranslation } from "react-i18next";
import { t } from "i18next";
import Contribute from "./pages/Contribute";
import { Helmet } from "react-helmet";

// Lazy
const FindChain = React.lazy(() => import("./pages/FindChain"));
Expand Down Expand Up @@ -232,6 +233,11 @@ export default function App() {
path={`${base}/admin/dashboard`}
component={AdminDashboard}
/>
<Route
exact
path={`${base}/500`}
component={InternalServerError}
/>
<Route path={`${base}/*`} component={FileNotFound} />
<Route path="*" component={I18nRedirect} />
</Switch>
Expand Down Expand Up @@ -263,13 +269,50 @@ function I18nRedirect() {

function FileNotFound() {
return (
<div className="max-w-screen-sm mx-auto flex-grow flex flex-col justify-center items-center">
<h1 className="font-serif text-secondary text-4xl font-bold my-10">
404 File not found
</h1>
<Link to="/" className="btn btn-primary">
{t("home")}
</Link>
</div>
<>
<Helmet>
<title>The Clothing Loop | 404</title>
<meta name="description" content="File not found" />
</Helmet>
<div className="max-w-screen-sm mx-auto flex-grow flex flex-col justify-center items-center">
<h1 className="font-serif text-secondary text-4xl font-bold my-10">
404 File not found
</h1>
<Link to="/" className="btn btn-primary">
{t("home")}
</Link>
</div>
</>
);
}

function InternalServerError() {
const l = useLocation<{ err?: any }>();

let err = l.state?.err;
if (err !== undefined && typeof err !== "string") {
if (typeof err?.message === "string") {
err = err.message;
}
if (typeof err?.data === "string") {
err = err.data;
}
}
return (
<>
<Helmet>
<title>The Clothing Loop | 500</title>
<meta name="description" content="Internal server error" />
</Helmet>
<div className="max-w-screen-sm mx-auto flex-grow flex flex-col justify-center items-center">
<h1 className="font-serif text-secondary text-4xl font-bold my-10">
500 Internal server error
</h1>
{err ? <p className="-mt-6 mb-10">{err}</p> : null}
<Link to="/" className="btn btn-primary">
{t("home")}
</Link>
</div>
</>
);
}
12 changes: 8 additions & 4 deletions frontend/src/providers/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { User } from "../api/types";
import { userGetByUID, userUpdate } from "../api/user";
import Cookies from "js-cookie";
import i18n from "../i18n";
import { Sleep } from "../util/sleep";

const IS_DEV_MODE = import.meta.env.DEV;

export enum UserRefreshState {
NeverLoggedIn,
LoggedIn,
ForceLoggedOut,
Offline,
}

export type AuthProps = {
Expand Down Expand Up @@ -113,10 +115,12 @@ export function AuthProvider({ children }: PropsWithChildren<{}>) {
i18n: i18n.language,
});
}
} catch (err) {
await authLogout().catch((err) => {
console.error("force logout failed:", err);
});
} catch (err: any) {
if (err?.status === 401) {
await authLogout().catch((err) => {
console.error("force logout failed:", err);
});
}
console.info("force logout");
return UserRefreshState.ForceLoggedOut;
}
Expand Down

0 comments on commit 1f9cd5a

Please sign in to comment.