el.xwx.moe/layouts/AuthRedirect.tsx

84 lines
2.7 KiB
TypeScript
Raw Normal View History

import { ReactNode, useEffect, useState } from "react";
import { useRouter } from "next/router";
2023-02-08 17:58:55 -06:00
import { useSession } from "next-auth/react";
import useInitialData from "@/hooks/useInitialData";
2024-07-31 13:15:50 -05:00
import { useUser } from "@/hooks/store/user";
2023-02-08 17:58:55 -06:00
2023-04-30 15:54:40 -05:00
interface Props {
children: ReactNode;
}
const stripeEnabled = process.env.NEXT_PUBLIC_STRIPE === "true";
export default function AuthRedirect({ children }: Props) {
2023-02-08 17:58:55 -06:00
const router = useRouter();
const { status } = useSession();
const [shouldRenderChildren, setShouldRenderChildren] = useState(false);
const { data: user = {} } = useUser();
2023-02-08 17:58:55 -06:00
useInitialData();
2023-04-30 15:54:40 -05:00
2023-07-19 23:46:49 -05:00
useEffect(() => {
const isLoggedIn = status === "authenticated";
const isUnauthenticated = status === "unauthenticated";
const isPublicPage = router.pathname.startsWith("/public");
const hasInactiveSubscription =
2024-10-26 08:42:21 -05:00
user.id &&
!user.subscription?.active &&
!user.parentSubscription?.active &&
stripeEnabled;
// There are better ways of doing this... but this one works for now
const routes = [
{ path: "/login", isProtected: false },
{ path: "/register", isProtected: false },
{ path: "/confirmation", isProtected: false },
{ path: "/forgot", isProtected: false },
{ path: "/auth/reset-password", isProtected: false },
{ path: "/", isProtected: false },
{ path: "/subscribe", isProtected: true },
{ path: "/dashboard", isProtected: true },
{ path: "/settings", isProtected: true },
{ path: "/collections", isProtected: true },
{ path: "/links", isProtected: true },
{ path: "/tags", isProtected: true },
{ path: "/preserved", isProtected: true },
{ path: "/admin", isProtected: true },
{ path: "/team", isProtected: true },
{ path: "/search", isProtected: true },
];
if (isPublicPage) {
setShouldRenderChildren(true);
} else {
if (isLoggedIn && hasInactiveSubscription) {
redirectTo("/subscribe");
2024-10-26 08:42:21 -05:00
} else if (isLoggedIn && !user.name && user.parentSubscriptionId) {
redirectTo("/member-onboarding");
2023-07-19 23:46:49 -05:00
} else if (
isLoggedIn &&
!routes.some((e) => router.pathname.startsWith(e.path) && e.isProtected)
2023-07-19 23:46:49 -05:00
) {
redirectTo("/dashboard");
2023-07-19 23:46:49 -05:00
} else if (
isUnauthenticated &&
routes.some((e) => router.pathname.startsWith(e.path) && e.isProtected)
2023-07-19 23:46:49 -05:00
) {
redirectTo("/login");
} else {
setShouldRenderChildren(true);
}
2023-07-19 23:46:49 -05:00
}
}, [status, user, router.pathname]);
2023-02-08 17:58:55 -06:00
function redirectTo(destination: string) {
router.push(destination).then(() => setShouldRenderChildren(true));
}
if (status !== "loading" && shouldRenderChildren) {
return <>{children}</>;
} else {
return <></>;
}
2023-02-08 17:58:55 -06:00
}