revite/src/context/revoltjs/CheckAuth.tsx

30 lines
746 B
TypeScript
Raw Normal View History

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