revite/src/context/revoltjs/CheckAuth.tsx

23 lines
572 B
TypeScript
Raw Normal View History

2021-06-18 15:07:26 -04:00
import { useContext } from "preact/hooks";
import { Redirect } from "react-router-dom";
2021-06-20 15:30:42 -04:00
import { Children } from "../../types/Preact";
2021-06-18 15:07:26 -04:00
2021-06-19 13:46:05 -04:00
import { OperationsContext } from "./RevoltClient";
2021-06-18 15:07:26 -04:00
interface Props {
auth?: boolean;
2021-06-20 15:30:42 -04:00
children: Children;
2021-06-18 15:07:26 -04:00
}
export const CheckAuth = (props: Props) => {
2021-06-19 13:46:05 -04:00
const operations = useContext(OperationsContext);
2021-06-18 15:07:26 -04:00
if (props.auth && !operations.ready()) {
return <Redirect to="/login" />;
} else if (!props.auth && operations.ready()) {
return <Redirect to="/" />;
}
return <>{props.children}</>;
};