2021-06-18 20:07:26 +01:00
|
|
|
import { Redirect } from "react-router-dom";
|
|
|
|
|
2021-12-11 21:04:12 +00:00
|
|
|
import { useApplicationState } from "../../mobx/State";
|
2021-07-05 11:23:23 +01:00
|
|
|
|
|
|
|
import { Children } from "../../types/Preact";
|
2021-12-11 21:04:12 +00:00
|
|
|
import { useClient } from "./RevoltClient";
|
2021-06-18 20:07:26 +01:00
|
|
|
|
|
|
|
interface Props {
|
2021-07-05 11:25:20 +01:00
|
|
|
auth?: boolean;
|
2021-12-28 12:24:35 +00:00
|
|
|
blockRender?: boolean;
|
|
|
|
|
2021-07-05 11:25:20 +01:00
|
|
|
children: Children;
|
2021-06-18 20:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const CheckAuth = (props: Props) => {
|
2021-12-11 21:04:12 +00:00
|
|
|
const auth = useApplicationState().auth;
|
|
|
|
const client = useClient();
|
2021-12-25 15:56:47 +00:00
|
|
|
const ready = auth.isLoggedIn() && !!client?.user;
|
2021-06-18 20:07:26 +01:00
|
|
|
|
2021-12-11 21:04:12 +00:00
|
|
|
if (props.auth && !ready) {
|
2021-12-28 12:24:35 +00:00
|
|
|
if (props.blockRender) return null;
|
2021-07-05 11:25:20 +01:00
|
|
|
return <Redirect to="/login" />;
|
2021-12-11 21:04:12 +00:00
|
|
|
} else if (!props.auth && ready) {
|
2021-12-28 12:24:35 +00:00
|
|
|
if (props.blockRender) return null;
|
2021-07-05 11:25:20 +01:00
|
|
|
return <Redirect to="/" />;
|
|
|
|
}
|
2021-06-18 20:07:26 +01:00
|
|
|
|
2021-07-05 11:25:20 +01:00
|
|
|
return <>{props.children}</>;
|
2021-06-18 20:07:26 +01:00
|
|
|
};
|