revite/src/context/revoltjs/CheckAuth.tsx

23 lines
572 B
TypeScript
Raw Normal View History

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