2024-05-20 18:23:11 -05:00
|
|
|
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";
|
2023-06-09 17:31:14 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-20 18:23:11 -05:00
|
|
|
const stripeEnabled = process.env.NEXT_PUBLIC_STRIPE === "true";
|
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default function AuthRedirect({ children }: Props) {
|
2023-02-08 17:58:55 -06:00
|
|
|
const router = useRouter();
|
2024-05-20 18:23:11 -05:00
|
|
|
const { status } = useSession();
|
|
|
|
const [shouldRenderChildren, setShouldRenderChildren] = useState(false);
|
2024-08-12 23:08:57 -05:00
|
|
|
const { data: user = {} } = useUser();
|
2023-02-08 17:58:55 -06:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
useInitialData();
|
2023-04-30 15:54:40 -05:00
|
|
|
|
2023-07-19 23:46:49 -05:00
|
|
|
useEffect(() => {
|
2024-05-20 18:23:11 -05:00
|
|
|
const isLoggedIn = status === "authenticated";
|
|
|
|
const isUnauthenticated = status === "unauthenticated";
|
|
|
|
const isPublicPage = router.pathname.startsWith("/public");
|
|
|
|
const hasInactiveSubscription =
|
2024-07-30 22:19:29 -05:00
|
|
|
user.id && !user.subscription?.active && stripeEnabled;
|
2024-05-22 19:56:56 -05:00
|
|
|
|
|
|
|
// There are better ways of doing this... but this one works for now
|
2024-05-20 18:23:11 -05:00
|
|
|
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 },
|
2024-05-24 16:12:47 -05:00
|
|
|
{ path: "/preserved", isProtected: true },
|
|
|
|
{ path: "/admin", isProtected: true },
|
2024-10-21 12:59:05 -05:00
|
|
|
{ path: "/team", isProtected: true },
|
2024-06-04 15:59:49 -05:00
|
|
|
{ path: "/search", isProtected: true },
|
2024-05-20 18:23:11 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
if (isPublicPage) {
|
|
|
|
setShouldRenderChildren(true);
|
|
|
|
} else {
|
|
|
|
if (isLoggedIn && hasInactiveSubscription) {
|
|
|
|
redirectTo("/subscribe");
|
2023-07-19 23:46:49 -05:00
|
|
|
} else if (
|
2024-05-20 18:23:11 -05:00
|
|
|
isLoggedIn &&
|
|
|
|
!routes.some((e) => router.pathname.startsWith(e.path) && e.isProtected)
|
2023-07-19 23:46:49 -05:00
|
|
|
) {
|
2024-05-20 18:23:11 -05:00
|
|
|
redirectTo("/dashboard");
|
2023-07-19 23:46:49 -05:00
|
|
|
} else if (
|
2024-05-20 18:23:11 -05:00
|
|
|
isUnauthenticated &&
|
2024-05-22 19:56:56 -05:00
|
|
|
routes.some((e) => router.pathname.startsWith(e.path) && e.isProtected)
|
2023-07-19 23:46:49 -05:00
|
|
|
) {
|
2024-05-20 18:23:11 -05:00
|
|
|
redirectTo("/login");
|
|
|
|
} else {
|
|
|
|
setShouldRenderChildren(true);
|
|
|
|
}
|
2023-07-19 23:46:49 -05:00
|
|
|
}
|
2024-07-30 22:19:29 -05:00
|
|
|
}, [status, user, router.pathname]);
|
2023-02-08 17:58:55 -06:00
|
|
|
|
2024-05-20 18:23:11 -05: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
|
|
|
}
|