mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
redirect on unauthenticated
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import { Fragment } from "react";
|
import { Fragment, useEffect } from "react";
|
||||||
import { Disclosure, Menu, Transition } from "@headlessui/react";
|
import { Disclosure, Menu, Transition } from "@headlessui/react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
@ -42,13 +42,37 @@ function classNames(...classes: any) {
|
|||||||
return classes.filter(Boolean).join(" ");
|
return classes.filter(Boolean).join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function useRedirectToLoginIfUnauthenticated() {
|
||||||
|
const { data: session, status } = useSession();
|
||||||
|
const loading = status === "loading";
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!loading && !session) {
|
||||||
|
router.replace({
|
||||||
|
pathname: "/login",
|
||||||
|
query: {
|
||||||
|
callbackUrl: `http://localhost:3000${location.pathname}${location.search}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [loading, session]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
loading: loading && !session,
|
||||||
|
session,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function Layout({ children }: any) {
|
export default function Layout({ children }: any) {
|
||||||
|
useRedirectToLoginIfUnauthenticated();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
navigation.forEach((element) => {
|
navigation.forEach((element) => {
|
||||||
element.current = router.route.startsWith("/" + element.href.split("/")[1]);
|
element.current = router.route.startsWith("/" + element.href.split("/")[1]);
|
||||||
});
|
});
|
||||||
|
|
||||||
const session = useSession();
|
const { status } = useSession();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@ -1,13 +1,18 @@
|
|||||||
import type { ReactElement } from "react";
|
import { NextPageContext } from "next";
|
||||||
import Layout from "../components/layout";
|
import { getSession } from "next-auth/react";
|
||||||
import type { NextPageWithLayout } from "./_app";
|
|
||||||
|
|
||||||
const Page: NextPageWithLayout = () => {
|
function RedirectPage() {
|
||||||
return <p className="text-red-900">This is the index page</p>;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
Page.getLayout = function getLayout(page: ReactElement) {
|
export async function getServerSideProps(context: NextPageContext) {
|
||||||
return <Layout>{page}</Layout>;
|
const session = await getSession(context);
|
||||||
};
|
|
||||||
|
|
||||||
export default Page;
|
if (!session?.user?.email) {
|
||||||
|
return { redirect: { permanent: false, destination: "/login" } };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { redirect: { permanent: false, destination: "/dashboard" } };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default RedirectPage;
|
||||||
|
|||||||
Reference in New Issue
Block a user